My WP Trek

Playing with WordPress


create_function is deprecated

Php function ‘create_function‘ has been deprecated as of PHP 7.2.0. So, while initializing widget I used to do this, add_action( ‘widgets_init’, create_function( ”, ‘register_widget(“Widget_Name”);’ ) ); Now, replacing deprecated function, add_action( ‘widgets_init’, function() { register_widget( ‘Widget_Name’ ); } );  

Using .js and .css file with WordPress

WordPress function used to register, enqueue and localize javascript and css files. add_action( ‘init’, ‘register_script_style’ ); function register_script_style() { wp_register_style( ‘your_style_name’, $url_to_your_css_folder . ‘/your-style.css’, array(), $version ); wp_register_script( ‘your_script_name’, $url_to_your_js_folder . ‘/your-script.js’, array( ‘jquery’ ), $version, true ); wp_enqueue_script( ‘your_script_name’ ); wp_enqueue_style( ‘your_style_name’ ); //Localize your script $params = array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ), […]

Quick Way of using AJAX in WordPress

This is a quick template that I repeatedly use while working with AJAX in WordPress.   In .js $(‘body’).on(‘click’, ‘.some_element_class’, function (e) { e.preventDefault(); var ajaxdata = {}; //Data that you’re sending ajaxdata.somedata = ‘somedata’; ajaxdata.action = ‘your_action_name’; $.ajax({ beforeSend: function () { //some work before sending data }, type: ‘POST’, dataType: ‘json’, url: ajaxscript.ajax_url, […]

Scroll to top