Home > Enterprise >  How to make the WP-Shopify only work on certain pages
How to make the WP-Shopify only work on certain pages

Time:05-29

I wondered if someone could help me with editing the wpshopify/wp-shopify.php in WordPress. My goal is to make the Shopify plugin work on certain pages and not run on other pages. So for example I would like the plugin to work on the [shop] page and not the [about us] page. I have seen some "Plugin Organizers" but unfortunately I couldn't make it work.

Does anyone have the experience or know-how to get this done?

CodePudding user response:

If you want to manage the styles and JavaScript of a plugin in WordPress so that on any page you just want to be loaded and used, I suggest using the following plugins.

  • WordPress Assets manager, dequeue scripts, dequeue styles
  • gonzales wp
  • Deactivate Plugins Per Page

But if you want to write a condition that you can manage, it means a specific plugin only when you want it to work like a specific page. For this you need to know the exact name of the plugin and then do it using a plugin management function.

CodePudding user response:

I wrote this code and tested it, it worked properly.

In this code, I first check the post ID, whether page or post or any other type of post.

Then I disable all plugin styles and scripts and delete the class that is attached to the body

Finally, I remove a new element created in a class to display the plugin root.

Put this code in the functions.php file

function disble_shopwp_pages()
{
    $ids = array(1, 2);  // option : id for post or page array( post id ) example : array(1, 2) or array(1)

    if (in_array(get_the_ID(), $ids)) {

        return true;
    }


}

function remove_wpshopify()
{


    if (disble_shopwp_pages()):

        wp_dequeue_style('shopwp-styles-frontend-all');
        wp_deregister_style('shopwp-styles-frontend-all');
        wp_dequeue_script('shopwp-runtime');
        wp_dequeue_script('shopwp-vendors-public');
        wp_dequeue_script('shopwp-public');


    endif;
}

add_action('wp_enqueue_scripts', 'remove_wpshopify', 9999);


function wpshopify_body_class($classes)
{

    if (disble_shopwp_pages()) {
        unset($classes[array_search('shopwp', $classes)]);
    }

    return $classes;
}

add_filter('body_class', 'wpshopify_body_class', 999, 2);

function remove_shopwp_root_elements()
{


    if (disble_shopwp_pages()) {

        echo '<script>
                jQuery(document).ready(function () {
                        jQuery("#shopwp-root").remove();
                });
              </script>';

    }
}

add_action('wp_footer', 'remove_shopwp_root_elements');
  • Related