WordPress disable "regenerator-runtime-js" and "wp-polyfill-js" - wordpress

These two srcripts are added on every footer of my WordPress pages. Is it possible to remove them via functions.php?

The following code in your functions.php should remove wp-polyfill and regenerator-runtime:
function deregister_polyfill(){
wp_deregister_script( 'wp-polyfill' );
wp_deregister_script( 'regenerator-runtime' );
}
add_action( 'wp_enqueue_scripts', 'deregister_polyfill');
See also: wp_deregister_script
Edit: After further investigation on the problem, I found that these styles are included by the Contact Form 7 plugin. My solution to the problem was to write a Must Use Plugin so that the Contact Form 7 plugin is only included when you are in the contact form.
For this purpose I have created the following file:
wp-content/mu-plugins/tk-mu-plugins.php
which is filled as follows:
<?php
/*
Plugin Name: TK-MU-Plugin
Description:
Author: Thomas Krakow
Version: 1.0
Author URI: https://thomas-krakow.de
*/
$fRequestURI = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
if( false === strpos( $fRequestURI, '/wp-admin/' ) ){
add_filter( 'option_active_plugins', 'tk_activate_plugins' );
}
function tk_activate_plugins( $plugins ){
global $fRequestURI;
$is_contact_page = strpos( $fRequestURI, '/contact/' );
$k = array_search( "contact-form-7/wp-contact-form-7.php", $plugins );
if( false !== $k && false === $is_contact_page ){
unset( $plugins[$k] );
}
return $plugins;
}
My Code at GitHub Gist: tk-mu-plugin.php
Explanation in German on my blog: CSS und Javascript nur bei Bedarf auf WordPress-Seiten einfügen

Related

Where can I put this code to add AMP pages support to WordPress without a plugin?

define( 'AMP_QUERY_VAR', apply_filters( 'amp_query_var', 'amp' ) );
add_rewrite_endpoint( AMP_QUERY_VAR, EP_PERMALINK );
add_filter( 'template_include', 'amp_page_template', 99 );
function amp_page_template( $template ) {
if( get_query_var( AMP_QUERY_VAR, false ) !== false ) {
if ( is_single() ) {
$template = get_template_directory() . '/amp-single.php';
}
}
return $template;
}
If you're looking to make your site AMP compatible without a plugin it would take a lot more work than the function referenced. Have you tried the official AMP plugin? I help out in the support forums if you want to post the question there, happy to help out from there.

Remove Contact Form 7 CSS and JS Unless Contact form 7 shortcode is used in the page

I want to show the css and javascript only when the shortcode is used in that page. If the short code not present in the wordpress page then the js and css of contact form should not be shown. For that what i have done is i have pasted the following code in my active themes function.php file.
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
The above code totally removes the js and css of contact form 7 plugin. What i need is if contact form 7 shortcode is pasted then both should be shown.
Here is the answer for your question. If there is not shortcode the css and js of contact form will be removed and if there is shortcode css and js will be added.
function rjs_lwp_contactform_css_js() {
global $post;
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'contact-form-7') ) {
wp_enqueue_script('contact-form-7');
wp_enqueue_style('contact-form-7');
}else{
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'rjs_lwp_contactform_css_js');
I needed the other version that corresponds widgets and shortcodes in theme file.
add_filter( 'wpcf7_load_css', '__return_false' );
add_filter( 'wpcf7_load_js', '__return_false' );
remove_action( 'wp_enqueue_scripts','wpcf7_recaptcha_enqueue_scripts', 20 );
add_filter('pre_do_shortcode_tag', 'enqueue_wpcf7_css_js_as_needed', 10, 2 );
function enqueue_wpcf7_css_js_as_needed ( $output, $shortcode ) {
if ( 'contact-form-7' == $shortcode ) {
wpcf7_recaptcha_enqueue_scripts();
wpcf7_enqueue_scripts();
wpcf7_enqueue_styles();
}
return $output;
}
You use below code.You can add your pages Id in this code.
function dvk_dequeue_scripts() {
$load_scripts = false;
if( is_singular() ) {
$post = get_post();
if( has_shortcode($post->post_content, 'contact-form-7') ) {
$load_scripts = true;
}
}
if( ! $load_scripts ) {
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'dvk_dequeue_scripts', 99 );
Reference

Wordpress - Setting custom template for multiple custom post types

I was wondering if anyone knew how to set up a single template for multiple custom post types. For example - I don't want to set up multiple templates that do the exact same thing.
Code
I found the following snippet while searching and it doesn't seem to work. I have placed this in functions.php in the theme I am using.
add_filter( 'single_template', function( $template ) {
$cpt = [ 'available-properties', 'leased-sold', 'norway' ];
return in_array( get_queried_object()->post_type, $cpt, true )
? 'path/to/country-single.php'
: $template;
} );
Found the answer
This seems to work great!
add_filter( 'template_include', function( $template )
{
// your custom post types
$my_types = array( 'available-properties', 'leased-sold' );
$post_type = get_post_type();
if ( ! in_array( $post_type, $my_types ) )
return $template;
return get_stylesheet_directory() . '/page-content__projects-single.php';
});

Automatically Deactivate Plugin After a Check

I'm trying to get my Wordpress plugin to deactivate automatically after a simple check. It seems to be calling the admin_notices method just fine, but the deactivate_plugin() method does not do anything. This is in the class constructor:
// End if the theme isn't compatible
if ( FALSE == $this->themesupport['support'] ) { // This test works fine
add_action( 'admin_init', array( &$this, 'deactivate_plugin' ) ); // Plugin doesn't deactivate
add_action( 'admin_notices', array( &$this, 'admin_notices' ) ); // I get notices
if ( isset( $_GET['activate'] ) )
unset( $_GET['activate'] );
return;
} // if()
The method is pretty straightforward:
public function deactivate_plugin() {
deactivate_plugins( plugin_basename( __FILE__ ) );
} // deactivate_plugin()
Putting an echo in that deactivate_plugin method and it gets called. I've also tried including the plugins.php file from core with no change.
The following works:
<?php
/**
* Plugin Name: (SO) Self-deactivate with $_GET['my_deactivate']
*/
add_action( 'admin_init', function()
{
if( isset( $_GET['my_deactivate'] ) )
{
deactivate_plugins( plugin_basename( __FILE__ ), true );
$url = admin_url( 'plugins.php?deactivate=true' );
header( "Location: $url" );
die();
}
});
After activating, enter any admin URL adding ?my_deactivate, e.g.:
http://example.com/wp-admin/users.php?my_deactivate
Reference:
Function deactivate_plugins does not exist
I was calling deactivate_plugins() from within a class, rather than the plugin file itself, which of course returned the wrong location for FILE

Remove submenu page "customize.php" in WordPress 4.0

When I was running WP 3.9.2 I was able to use the following code to remove the Customize menu item from Appearance in the admin menu.
function remove_customize() {
remove_submenu_page('themes.php', 'customize.php');
}
add_action('admin_init', 'remove_customize', 999);
Once I updated to 4.0 this is no longer working.
This works with WordPress 4.1 and 4.0 and 3.x here:
Edit: Adjusted for WordPress 4.1 compatibility:
function remove_customize() {
$customize_url_arr = array();
$customize_url_arr[] = 'customize.php'; // 3.x
$customize_url = add_query_arg( 'return', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'customize.php' );
$customize_url_arr[] = $customize_url; // 4.0 & 4.1
if ( current_theme_supports( 'custom-header' ) && current_user_can( 'customize') ) {
$customize_url_arr[] = add_query_arg( 'autofocus[control]', 'header_image', $customize_url ); // 4.1
$customize_url_arr[] = 'custom-header'; // 4.0
}
if ( current_theme_supports( 'custom-background' ) && current_user_can( 'customize') ) {
$customize_url_arr[] = add_query_arg( 'autofocus[control]', 'background_image', $customize_url ); // 4.1
$customize_url_arr[] = 'custom-background'; // 4.0
}
foreach ( $customize_url_arr as $customize_url ) {
remove_submenu_page( 'themes.php', $customize_url );
}
}
add_action( 'admin_menu', 'remove_customize', 999 );
Answer should be:
add_action( 'admin_menu', function () {
global $submenu;
if ( isset( $submenu[ 'themes.php' ] ) ) {
foreach ( $submenu[ 'themes.php' ] as $index => $menu_item ) {
foreach ($menu_item as $value) {
if (strpos($value,'customize') !== false) {
unset( $submenu[ 'themes.php' ][ $index ] );
}
}
}
}
});
The way rjb used an array as the needle in in_array() in the accepted answer doesn't work. Check out why in the docs. I replaced in_array with another foreach that loops through the $menu_item arrays and looks for 'customize' as part of the value.
Works for me with WordPress 4.9.6
You can directly modify the $submenus global like so:
global $submenu;
unset($submenu['themes.php'][6]); // Customize link
I'm using this in the same function, hooked into admin_menu, as I use to unset other admin items and it seems to be working fine
function as_remove_menus () {
remove_menu_page('upload.php'); //hide Media
remove_menu_page('link-manager.php'); //hide links
remove_submenu_page( 'edit.php', 'edit-tags.php' ); //hide tags
global $submenu;
// Appearance Menu
unset($submenu['themes.php'][6]); // Customize
}
add_action('admin_menu', 'as_remove_menus');
Edit: Updated for WordPress 4.9+ and increased compatibility with PHP <= 5.4
WordPress core doesn't offer a hook to natively disable the theme customizer, but there is a clever and elegant way to remove the “Customize” link from the Appearance menu by altering the global $submenu variable:
/**
* Remove Admin Menu Link to Theme Customizer
*/
add_action( 'admin_menu', function () {
global $submenu;
if ( isset( $submenu[ 'themes.php' ] ) ) {
foreach ( $submenu[ 'themes.php' ] as $index => $menu_item ) {
if ( in_array( array( 'Customize', 'Customizer', 'customize' ), $menu_item ) ) {
unset( $submenu[ 'themes.php' ][ $index ] );
}
}
}
});
While other code samples here and elsewhere irresponsibly rely on specific numeric indexes of the global $submenu variable (e.g. $submenu['themes.php'][6][0], ...), this method intelligently traverses through the hierarchy so it should be compatible with older (3.x) and newer versions of WordPress (4.x) alike.
You can in fact use remove_submenu_page to remove the theme submenu option from the admin screen. The trick is that the url must match what is being exactly linked in the admin for that function to work.
function remove_admin_menus() {
remove_submenu_page(
'themes.php',
'customize.php?return=' .
urlencode( str_replace( get_bloginfo('url'), "", get_admin_url() ) ) .
'themes.php' );
}
add_action( 'admin_init', 'remove_admin_menus' );
I have programmatically determined the admin url in the case that you aren't simply using '/wp-admin'. #isabisa This will also avoid breaking in the future if the index of the menu item ever changes.
I'm using this in WP 4.0 and it works great!
Removing the menu is only a halfway solution, since it doesn't completely disable the customizer. In order to fully and securely disable the customizer (and also remove the menu), you need to remove the customizer permission from all users. Something like this would do it:
add_filter('map_meta_cap', function($caps, $cap, $user_id, $args) {
if ('customize' == $cap) return ['do_not_allow'];
return $caps;
}, 10, 4);
WordPress >= 4.9.8
add_action('admin_menu', function () {
$request = urlencode($_SERVER['REQUEST_URI']);
remove_submenu_page('themes.php', 'customize.php?return='. $request);
}, 999);
The accepted answer by #rjb didn't work for my spanish wordpress, but just changing the Customize to customize did the trick.
/**
* Remove Admin Menu Link to Theme Customizer
*/
add_action( 'admin_menu', function () {
global $submenu;
if ( isset( $submenu[ 'themes.php' ] ) ) {
foreach ( $submenu[ 'themes.php' ] as $index => $menu_item ) {
if ( in_array( 'customize', $menu_item ) ) {
unset( $submenu[ 'themes.php' ][ $index ] );
}
}
}
});
Works in wordpres 5.*
Removing Customize from Wordpress Admin you need to remove from the sidebar and from the top bar in front end as well
From the Sidebar Menu
add_action( 'admin_menu', 'remove_customize' );
function remove_customize() {
global $submenu;
if ( isset( $submenu[ 'themes.php' ] ) ) {
foreach ( $submenu[ 'themes.php' ] as $index => $menu_item ) {
if(in_array('Customize', $menu_item) || in_array('Customizer', $menu_item) || in_array('customize', $menu_item))
{
unset( $submenu[ 'themes.php' ][ $index ] );
}
}
}
}
From Admin bar in top (In The front End)
add_action( 'admin_bar_menu', 'remove_customize_menu_bar', 999 );
function remove_customize_menu_bar( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'customize' );
}
This will completely disable the customize option :)
For WordPress 5
add_action( 'admin_menu', function() {
remove_submenu_page( 'themes.php', 'customize.php?return=' . urlencode($_SERVER['SCRIPT_NAME']));
}, 999 )
Try changing 'admin_init' in 'admin_menu'
#bash88 answer and #Emanuel A. answer works but if you want also remove buttons (blue customize buttons) from themes page answer should be:
Tested WordPress 5.0.3
/**
* Remove customize links from admin panel.
*/
function admin_remove_customize_links() {
echo '<style>.hide-if-no-customize { display: none !important; }</style>';
}
add_action( 'admin_head', 'admin_remove_customize_links' );
Update of the approved response (Wordpress 5)
add_action( 'admin_menu', 'rompiot_remove_customize' );
/**
* Remove Admin Menu Link to Theme Customizer
*/
public function rompiot_remove_customize()
{
global $submenu;
if (isset($submenu['themes.php'])) {
foreach ($submenu['themes.php'] as $index => $array_menu_item) {
foreach ($array_menu_item as $key => $menu_item) {
if (in_array($menu_item, ['Customize', 'Customizer', 'customize'])) {
unset($submenu['themes.php'][$index]);
}
}
}
}
}
FUNCTIONING CODE AS OF WORDPRESS 5.7+, AUGUST 2021
I needed to customize the WP admin bar for a large WP site we are working on (text was running off the admin bar when resizing). I tried literally every snippet of code from all the previous answers to this question; unfortunately none worked for me. This code I found off Google did, so I wanted to share with others (goes in your functions.php):
/**
* This function removes items from the WP admin bar. If it gets too cluttered,
* things will run off the screen and look bad.
* #param object $wp_admin_bar representing the WP admin bar.
*/
function remove_from_admin_bar($wp_admin_bar) {
// WordPress Core Items (uncomment to remove)
$wp_admin_bar->remove_node('updates');
$wp_admin_bar->remove_node('comments');
//wp_admin_bar->remove_node('new-content');
$wp_admin_bar->remove_node('wp-logo');
//$wp_admin_bar->remove_node('site-name');
//$wp_admin_bar->remove_node('my-account');
//$wp_admin_bar->remove_node('search');
$wp_admin_bar->remove_node('customize');
}
add_action('admin_bar_menu', 'remove_from_admin_bar', 999);
I think a great start is to remove the comments, updates, and WP icon. Custom plugins can be disabled here, too. The 999 indicates when the hook will fire off later. You can wrap elements in the is_admin() function if you want to hide or show different links on the front end vs. the WP admin.

Resources