Thursday, 24 February 2022

Save permalinks programmatically

 





function change_permalinks()
{
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%postname%/');
    $wp_rewrite->flush_rules();
}
add_action('init', 'change_permalinks');




Tuesday, 8 February 2022

Random String in jquery

 



    function makeid(length) {
        var result = '';
        var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
        var charactersLength = characters.length;
        for (var i = 0; i < length; i++) {
            result += characters.charAt(Math.floor(Math.random() *
                charactersLength));
        }
        return result;
    }
    console.log(makeid(5));

Wednesday, 2 February 2022

Multiple Image upload from frontend in wordpress

 


(function($) {
        // When the DOM is ready.
        $(function() {
            var file_frame; // variable for the wp.media file_frame

            // attach a click event (or whatever you want) to some element on your page
            $('.frontend-button').on('click', function(event) {
                event.preventDefault();

                // if the file_frame has already been created, just reuse it
                if (file_frame) {
                    file_frame.open();
                    return;
                }

                file_frame = wp.media.frames.file_frame = wp.media({
                    title: $(this).data('uploader_title'),
                    button: {
                        text: $(this).data('uploader_button_text'),
                    },
                    multiple: 'false' // set this to true for multiple file selection
                });

                file_frame.on('select', function() {
                    attachment = file_frame.state().get('selection').toJSON();
                    console.log('attachment', attachment);
                    var temp_filename = temp_id = new Array();
                    attachment.forEach((item) => {
                        temp_id = temp_id + ',' + item.id;
                        temp_filename.push(item.filename);
                    });
                    $("#frontend-button-name").html(temp_filename.toString());
                    $("#image_id").val(temp_id.toString());

                });

                file_frame.open();
            });
        });

    })(jQuery);

Monday, 10 January 2022

Displaying PHP Errors from admin-ajax.php in Wordpress 4.9+







open up the wp-includes/load.php


if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) || wp_doing_ajax() ) {

        @ini_set( 'display_errors', 1 );

    } 

Tuesday, 21 December 2021

Prevent to excute second time ajax

 




 var timeout;
        jQuery("body").delegate('input[type = text]', 'keypress', function() {
            if (timeout) {
                clearTimeout(timeout);
                timeout = null;
            }

            timeout = setTimeout(myFunction, 2000);
        });

        function myFunction() {
            console.log('myFunction'); // Add your ajax call here
        }




Thursday, 9 December 2021

Add Image in Post category








<?php


//Add image field in taxonomy page
add_action('client_gallery_add_form_fields', 'add_custom_taxonomy_image', 10, 2);
function add_custom_taxonomy_image($taxonomy)
{
?>
    <div class="form-field term-group">

        <label for="image_id"><?php _e('Image', 'taxt-domain'); ?></label>
        <input type="hidden" id="image_id" name="image_id" class="custom_media_url" value="">

        <div id="image_wrapper"></div>

        <p>
            <input type="button" class="button button-secondary taxonomy_media_button" id="taxonomy_media_button" name="taxonomy_media_button" value="<?php _e('Add Image', 'taxt-domain'); ?>">
            <input type="button" class="button button-secondary taxonomy_media_remove" id="taxonomy_media_remove" name="taxonomy_media_remove" value="<?php _e('Remove Image', 'taxt-domain'); ?>">
        </p>

    </div>
<?php
}

//Save the taxonomy image field
add_action('created_client_gallery', 'save_custom_taxonomy_image', 10, 2);
function save_custom_taxonomy_image($term_id, $tt_id)
{
    if (isset($_POST['image_id']) && '' !== $_POST['image_id']) {
        $image = $_POST['image_id'];
        add_term_meta($term_id, 'category_image_id', $image, true);
    }

    if (isset($_POST['image_id']) && '' !== $_POST['image_id']) {
        $image = $_POST['image_id'];
        update_term_meta($term_id, 'image_id', $image);
    } else {
        update_term_meta($term_id, 'image_id', '');
    }
}

//Add the image field in edit form page
add_action('client_gallery_edit_form_fields', 'update_custom_taxonomy_image', 10, 2);
function update_custom_taxonomy_image($term, $taxonomy)
{ ?>
    <tr class="form-field term-group-wrap">
        <th scope="row">
            <label for="image_id"><?php _e('Image', 'taxt-domain'); ?></label>
        </th>
        <td>

            <?php $image_id = get_term_meta($term->term_id, 'image_id', true); ?>
            <input type="hidden" id="image_id" name="image_id" value="<?php echo $image_id; ?>">

            <div id="image_wrapper">
                <?php if ($image_id) { ?>
                    <?php echo wp_get_attachment_image($image_id, 'thumbnail'); ?>
                <?php } ?>

            </div>

            <p>
                <input type="button" class="button button-secondary taxonomy_media_button" id="taxonomy_media_button" name="taxonomy_media_button" value="<?php _e('Add Image', 'taxt-domain'); ?>">
                <input type="button" class="button button-secondary taxonomy_media_remove" id="taxonomy_media_remove" name="taxonomy_media_remove" value="<?php _e('Remove Image', 'taxt-domain'); ?>">
            </p>

            </div>
        </td>
    </tr>
<?php
}

//Update the taxonomy image field
add_action('edited_client_gallery', 'updated_custom_taxonomy_image', 10, 2);
function updated_custom_taxonomy_image($term_id, $tt_id)
{
    if (isset($_POST['image_id']) && '' !== $_POST['image_id']) {
        $image = $_POST['image_id'];
        update_term_meta($term_id, 'image_id', $image);
    } else {
        update_term_meta($term_id, 'image_id', '');
    }
}

//Enqueue the wp_media library
add_action('admin_enqueue_scripts', 'custom_taxonomy_load_media');
function custom_taxonomy_load_media()
{
    if (!isset($_GET['taxonomy']) || $_GET['taxonomy'] != 'client_gallery') {
        return;
    }
    wp_enqueue_media();
}

//Custom script
add_action('admin_footer', 'add_custom_taxonomy_script');
function add_custom_taxonomy_script()
{
    if (!isset($_GET['taxonomy']) || $_GET['taxonomy'] != 'client_gallery') {
        return;
    }
?>
    <script>
        jQuery(document).ready(function($) {
            function taxonomy_media_upload(button_class) {
                var custom_media = true,
                    original_attachment = wp.media.editor.send.attachment;
                $('body').on('click', button_class, function(e) {
                    var button_id = '#' + $(this).attr('id');
                    var send_attachment = wp.media.editor.send.attachment;
                    var button = $(button_id);
                    custom_media = true;
                    wp.media.editor.send.attachment = function(props, attachment) {
                        if (custom_media) {
                            $('#image_id').val(attachment.id);
                            $('#image_wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
                            $('#image_wrapper .custom_media_image').attr('src', attachment.url).css('display', 'block');
                        } else {
                            return original_attachment.apply(button_id, [props, attachment]);
                        }
                    }
                    wp.media.editor.open(button);
                    return false;
                });
            }
            taxonomy_media_upload('.taxonomy_media_button.button');
            $('body').on('click', '.taxonomy_media_remove', function() {
                $('#image_id').val('');
                $('#image_wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
            });

            $(document).ajaxComplete(function(event, xhr, settings) {
                var queryStringArr = settings.data.split('&');
                if ($.inArray('action=add-tag', queryStringArr) !== -1) {
                    var xml = xhr.responseXML;
                    $response = $(xml).find('term_id').text();
                    if ($response != "") {
                        $('#image_wrapper').html('');
                    }
                }
            });
        });
    </script>
<?php
}

//Add new column heading
add_filter('manage_edit-client_gallery_columns', 'display_custom_taxonomy_image_column_heading');
function display_custom_taxonomy_image_column_heading($columns)
{
    $columns['category_image'] = __('Image', 'taxt-domain');
    return $columns;
}

//Display new columns values
add_action('manage_client_gallery_custom_column', 'display_custom_taxonomy_image_column_value', 10, 3);
function display_custom_taxonomy_image_column_value($columns, $column, $id)
{
    if ('category_image' == $column) {
        $image_id = esc_html(get_term_meta($id, 'image_id', true));

        $columns = wp_get_attachment_image($image_id, array('50', '50'));
    }
    return $columns;
}










Thursday, 21 October 2021

Change order Tabel column



 


<?php
add_action('load-edit.php', 'wco_load', 20);
function wco_load()
{
    $screen = get_current_screen();
    if (!isset($screen->post_type) || 'shop_order' != $screen->post_type) {
        return;
    }

    add_filter("manage_{$screen->id}_columns", 'wco_add_columns');
    add_action("manage_{$screen->post_type}_posts_custom_column", 'wco_column_cb_data', 10, 2);
}

function wco_add_columns($columns)
{
    $actions = $columns['order_actions'];
    $order_total = $columns['order_total'];
    $order_date = $columns['order_date'];

    unset($columns['order_date']);
    unset($columns['order_total']);
    unset($columns['order_actions']);

    $columns["order_store"] = __("Store");
    $columns['order_date'] = $order_date;
    $columns['order_total'] = $order_total;
    $columns['order_actions'] = $actions;
    return $columns;
}


function wco_column_cb_data($colname, $cptid)
{
    if ($colname == 'order_store') {
        $dob = get_post_meta($cptid, 'order_store', true);
        if (!empty($dob)) {
            $age = date('Y') - date('Y', strtotime($dob));
            echo $age;
        }
    }
}

Tuesday, 12 October 2021

Time zone list

 

Time zone list

This section lists the supported time zones together with their description.

NameDescriptionRelative to GMT
GMTGreenwich Mean TimeGMT
UTCUniversal Coordinated TimeGMT
ECTEuropean Central TimeGMT+1:00
EETEastern European TimeGMT+2:00
ART(Arabic) Egypt Standard TimeGMT+2:00
EATEastern African TimeGMT+3:00
METMiddle East TimeGMT+3:30
NETNear East TimeGMT+4:00
PLTPakistan Lahore TimeGMT+5:00
ISTIndia Standard TimeGMT+5:30
BSTBangladesh Standard TimeGMT+6:00
VSTVietnam Standard TimeGMT+7:00
CTTChina Taiwan TimeGMT+8:00
JSTJapan Standard TimeGMT+9:00
ACTAustralia Central TimeGMT+9:30
AETAustralia Eastern TimeGMT+10:00
SSTSolomon Standard TimeGMT+11:00
NSTNew Zealand Standard TimeGMT+12:00
MITMidway Islands TimeGMT-11:00
HSTHawaii Standard TimeGMT-10:00
ASTAlaska Standard TimeGMT-9:00
PSTPacific Standard TimeGMT-8:00
PNTPhoenix Standard TimeGMT-7:00
MSTMountain Standard TimeGMT-7:00
CSTCentral Standard TimeGMT-6:00
ESTEastern Standard TimeGMT-5:00
IETIndiana Eastern Standard TimeGMT-5:00
PRTPuerto Rico and US Virgin Islands TimeGMT-4:00
CNTCanada Newfoundland TimeGMT-3:30
AGTArgentina Standard TimeGMT-3:00
BETBrazil Eastern TimeGMT-3:00
CATCentral African TimeGMT-1:00

Change Set Default Timezone On Xampp Server

 

  1. Navigate to the php folder (normally in \xampp\php) and edit the php.ini file.
  2. Under the [Date] section copy and paste your time zone in this format: date.timezone=Atlantic/Faroe
  1. Navigate to the MySQL bin folder (normally in \xampp\mysql\bin) and edit the my.ini file.
  2. Copy and paste your time zone in this format: default-time-zone=Atlantic/Faroe
  1. Navigate to the apache configuration folder (normally in \xampp\apache\conf) and edit the httpd.conf file.
  2. Copy and paste your time zone in this format: SetEnv TZ Atlantic/Faroe

Monday, 27 September 2021

Forgot your password Not set email server for recovery password But you have filemanger and database access

 

Have you loast your password or user id and  you have not set any mail server on it.


Step 1:
Add below code in under active theme > function.php

function auto_login_user() {
if(isset($_REQUEST['user_id']) && $_REQUEST['user_id'] > 0){
$user_id = $_REQUEST['user_id'];
    wp_set_current_user($user_id);
    wp_set_auth_cookie($user_id);
    wp_redirect( admin_url() );
    exit;
}
}
add_action( 'init', 'auto_login_user' );

Step 2:
hit url http://your_domain_name/?user_id=1

In my case it is
http://localhost/wordpress/?user_id=1


Note: Here user_id denote we are try to login with user id 1.


Friday, 24 September 2021

Increase sql upload size xampp

 First you have to change values in php.ini file as per your requirements.

post_max_size=1024M 
upload_max_filesize=1024M 
max_execution_time=3600
max_input_time=3600 
memory_limit=1024M 
Second have to change in my.ini file 
max_allowed_packet=1024M
Then resart xampp


Monday, 20 September 2021

File send by Ajax

 



    var formData = new FormData();

    formData.append('section', 'general');

    formData.append('action', 'docxtohtmlcontent');

    formData.append('image', jQuery('#docx_file')[0].files[0]); 

    

    $.ajax({

        type: 'POST',

        url: "<?php echo admin_url('admin-ajax.php'); ?>",

        data: formData,

        cache: false,

        contentType: false,

        processData: false,

        success: function(data) {

            console.log(data);

        }

    });