PHP 7.3 Function create_function() is deprecated - wordpress

I have used create_function in my theme below.
add_action( 'widgets_init', create_function( '', 'register_widget( "Woocommerce_Header_Cart" );' ) );
But for PHP 7.3.0, the create_function() is deprecated.
Any idea, how to fix my codes above on PHP 7.3.0.
Thanks for your help,

Try this code
add_action( 'widgets_init', 'custom_widget_func');
funcation custom_widget_func(){
register_widget( "Woocommerce_Header_Cart" );
}

Replace
add_action( 'widgets_init', create_function( '', 'register_widget( "Woocommerce_Header_Cart" );' ) );
with this, using an anonymous function instead :
add_action( 'widgets_init', function() { return register_widget("Woocommerce_Header_Cart"); } );

Related

wp_enqueue_style doesn't work but wp_enqueue_scripts works

I have a code like this
function fs_express_theme_styles() {
wp_enqueue_style( 'fs-express-theme-style', get_stylesheet_uri(), array(), filemtime(get_stylesheet_uri()) );
}
add_action( 'wp_enqueue_style', 'fs_express_theme_styles' );
that doesn't work. I tried different versioning and no versioning at all. As a result I don't get any errors in the console or debug files but styles are not loaded.
However, this worked:
function fs_express_theme_scripts() {
wp_enqueue_style( 'fs-express-theme-style', get_stylesheet_uri(), array(), filemtime(get_stylesheet_uri()) );
}
add_action( 'wp_enqueue_scripts', 'fs_express_theme_scripts' );
I am not sure why add_action( 'wp_enqueue_style', 'fs_express_theme_styles' ) didn't work but add_action( 'wp_enqueue_scripts', 'fs_express_theme_scripts' ) worked.
Can someone, please, explain?
At the beggining I tried putting everything in two separate functions like this:
// We define the function:
function MYTHEME_scripts() {
wp_enqueue_script('jquery-ui-datepicker');
}
// Add the functions to WP loading list.
add_action( 'wp_enqueue_scripts', 'MYTHEME_scripts' );
function MYTHEME_styles() {
wp_enqueue_style('jquery-ui-css', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css');
wp_enqueue_style('jquery-ui-css', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
}
// Add the functions to WP loading list.
add_action( 'wp_enqueue_style', 'MYTHEME_styles' );
that was taken from here https://wordpress.stackexchange.com/questions/137104/wp-enqueue-script-was-called-incorrectly, but it didn't work so while testing I figured out that loading styles through wp_enqueue_scripts worked
I also added this
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = get_stylesheet_uri();
fwrite($myfile, $txt);
fclose($myfile);
to both functions to test things out and I see that add_action( 'wp_enqueue_style', 'fs_express_theme_styles' ) never worked but add_action( 'wp_enqueue_scripts', 'fs_express_theme_scripts' ) did

Remove photoswipe-css and photoswipe-default-skin-css in WordPress

I am building a custom webshop and I am trying to remove the photoswipe style documents but I can't seem to get rid of them, I tried a couple of "solutions" that I found online but they are not working. This is what I tried so far in the functions.php:
remove_theme_support( 'wc-product-gallery-zoom' );
remove_theme_support( 'wc-product-gallery-lightbox' );
remove_theme_support( 'wc-product-gallery-slider' );
and
add_filter( 'woocommerce_enqueue_styles', 'wpf_dequeue_styles' );
function wpf_dequeue_styles( $enqueue_styles ) {
unset( $enqueue_styles['photoswipe-css'] );
unset( $enqueue_styles['photoswipe-default-skin-css'] );
return $enqueue_styles;
}
and
wp_dequeue_script('photoswipe-css');
wp_dequeue_script('photoswipe-default-skin-css');
Can someone help me finding the solution? I don't have any images in my webshop so I do not need these scripts.
You can remove_theme_support using after_setup_theme action hook.
add_action( 'after_setup_theme', 'remove_photoswipe_css', 11 );
function remove_photoswipe_css() {
remove_theme_support( 'wc-product-gallery-zoom' );
remove_theme_support( 'wc-product-gallery-lightbox' );
remove_theme_support( 'wc-product-gallery-slider' );
}
You can wp_dequeue_style using wp_enqueue_scripts action hook.
add_action( 'wp_enqueue_scripts', 'dequeue_photoswipe_css', 11 );
function dequeue_photoswipe_css() {
wp_dequeue_style( 'photoswipe-css' );
wp_dequeue_style( 'photoswipe-default-skin-css' );
}
If anyone is still looking for the answer to this then the correct code would be :
add_action( 'wp_enqueue_scripts', 'dequeue_photoswipe_css', 99);
function dequeue_photoswipe_css() {
wp_dequeue_style( 'photoswipe' );
wp_dequeue_style( 'photoswipe-default-skin-css' );
wp_dequeue_script( 'photoswipe-ui-default' );
}

How to remove gutenberg_render_title_tag from theme function file?

I have try remove action but not working.
add_action( 'wp_loaded', 'remove_my_action' );
function remove_my_action() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', -1 );
}
or
add_action( 'init', 'remove_my_action' );
function remove_my_action() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', -1 );
}
i am using yoast plugin. Yoast plugin also using remove action https://github.com/Yoast/wordpress-seo/blob/trunk/src/integrations/front-end-integration.php#L220 but it's not working. Still tag coming two time. i wants to remove gutenberg title tag.
I got the solution for this.
add_action( 'wp', 'remove_default_guten_title' );
function remove_default_guten_title() {
foreach ( gutenberg_get_template_type_slugs() as $template_type ) {
add_filter( str_replace( '-', '', $template_type ) . '_template', 'remove_default_title', 21, 3 );
}
}
function remove_default_title() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
return gutenberg_dir_path() . 'lib/template-canvas.php';
}

How to Globally Use wp_localize_script() Ajax URL

I have added this to my functions.php and need to use ajaxURL in all of enqueued scripts in the template (instead of enqueuing only one script here
add_action( 'wp_enqueue_scripts', 'ajaxify_enqueue_scripts' );
function ajaxify_enqueue_scripts() {
wp_localize_script( 'ajaxify', 'ajaxURL', array('ajax_url' => get_template_directory_uri() . '/app/login.php' ));
}
add_action( 'wp_ajax_nopriv_set_ajaxify', 'set_ajaxify' );
add_action( 'wp_ajax_set_ajaxify', 'set_ajaxify' );
but when I try to call an ajax method I am getting this error
Uncaught ReferenceError: ajaxURL is not defined
Is there any way to add the ajaxURL to all scripts?
Try this code
add_action( 'wp_enqueue_scripts', 'ajaxify_enqueue_scripts' );
function ajaxify_enqueue_scripts() {
wp_enqueue_script('ajaxify', get_template_directory_uri() . '/test.js');
wp_localize_script( 'ajaxify', 'ajaxURL', array('ajax_url' => admin_url('admin-ajax.php')));
}
Put This Code :
function your_function_name() {
wp_register_script('ajaxify', get_template_directory_uri() . '/your-jsname.js', array('jquery'), '', true);
wp_enqueue_script('ajaxify');
wp_localize_script('ajaxify', 'ajaxURL', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ),'siteurl' => site_url() ) );
}
add_action( 'wp_enqueue_scripts', 'your_function_name' );

WordPress plugin notifications

I am developing a plugin for my clients that advises me when Core, Plugin, or Theme updates are available and emails this to me. I would like to change the default update notification text when this plugin is activated to something like "Plugin update is available however, updates are managed by company xyz".
// Disable core updates
# 2.3 to 2.7:
add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
# 2.8 to 3.0:
remove_action( 'wp_version_check', 'wp_version_check' );
remove_action( 'admin_init', '_maybe_update_core' );
add_filter( 'pre_transient_update_core', create_function( '$a', "return null;" ) );
# 3.0:
add_filter( 'pre_site_transient_update_core', create_function( '$a', "return null;" ) );
// Disable plugin updates
# 2.3 to 2.7:
add_action( 'admin_menu', create_function( '$a', "remove_action( 'load-plugins.php', 'wp_update_plugins' );") );
# Why use the admin_menu hook? It's the only one available between the above hook being added and being applied
add_action( 'admin_init', create_function( '$a', "remove_action( 'admin_init', 'wp_update_plugins' );"), 2 );
add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_update_plugins' );"), 2 );
add_filter( 'pre_option_update_plugins', create_function( '$a', "return null;" ) );
# 2.8 to 3.0:
remove_action( 'load-plugins.php', 'wp_update_plugins' );
remove_action( 'load-update.php', 'wp_update_plugins' );
remove_action( 'admin_init', '_maybe_update_plugins' );
remove_action( 'wp_update_plugins', 'wp_update_plugins' );
add_filter( 'pre_transient_update_plugins', create_function( '$a', "return null;" ) );
# 3.0:
remove_action( 'load-update-core.php', 'wp_update_plugins' );
add_filter( 'pre_site_transient_update_plugins', create_function( '$a', "return null;" ) );
This isn't really an answer to the question, but the above code is unfortunately all over the net and it's badly incorrect. While returning null to the pre_site_ filters does suppress the notification messages, it also forces wordpress to check for updates continuously, because it can't tell when the last update check was made.
The code below (5.3+) implements a better version that suppresses the messages without making WP run update checks on every single WP admin request.
$func = function ($a) {
global $wp_version;
return (object) array(
'last_checked' => time(),
'version_checked' => $wp_version,
);
};
add_filter('pre_site_transient_update_core', $func);
add_filter('pre_site_transient_update_plugins', $func);
add_filter('pre_site_transient_update_themes', $func);

Resources