0

I am trying to remove action that is added via plugin in my theme. The below plugin class creates a portfolio page template but I don't want this page template in my theme. Is there any way to unhook or remove it?

Below is the plugin class:

class Raratheme_Companion_Templates
    {
        function init()
        {
            add_filter( 'template_include', array( $this, 'rrtc_include_template_function' ) );
            add_action( 'wp_loaded', array( $this, 'rrtc_add_portfolio_templates' ) );
        }
        /**
         * Portfolio template.
        */
        function rrtc_get_portfolio_template( $template ) {
            $post = get_post();
            $page_template = get_post_meta( $post->ID, '_wp_page_template', true );
            if( $page_template == 'templates/portfolio.php' ){
                if ( $theme_file = locate_template( 'templates/portfolio.php' ) ) {
                    return $theme_file;
                } else {
                    return RARATC_BASE_PATH . '/includes/templates/portfolio.php';
                }
            }
            return $template;
        }

        /**
         * Template over-ride for single portfolio.
         *
         * @since    1.0.0
         */
        function rrtc_include_template_function( $template_path ) {
            if ( get_post_type() == 'rara-portfolio' ) {
                if ( is_single() ) {
                    if ( $theme_file = locate_template( 'single-rara-portfolio.php' ) ) {
                        $template_path = $theme_file;
                    } else {
                        $template_path = RARATC_BASE_PATH . '/includes/templates/single-rara-portfolio.php';
                    }
                }
            }
            return $template_path;
        }


        /**
         * Portfolio template returned.
        */
        function rrtc_filter_admin_page_templates( $templates ) {
            $templates['templates/portfolio.php'] = __( 'Portfolio Template', 'raratheme-companion' );
            return $templates;
        }

        /**
         * Portfolio template added.
        */
        function rrtc_add_portfolio_templates() {
            if( is_admin() ) {
                add_filter( 'theme_page_templates', array($this, 'rrtc_filter_admin_page_templates' ) );
            }
            else {
                add_filter( 'page_template', array( $this, 'rrtc_get_portfolio_template' ) );
            }
        }
    }
    $obj = new Raratheme_Companion_Templates;
    $obj->init();

I tried this with no success:

function def()
{
    // global $obj;
    // $obj = new Raratheme_Companion_Templates;
    if( is_admin() ) {
        remove_filter( 'theme_page_templates', array('Raratheme_Companion_Templates', 'rrtc_filter_admin_page_templates' ) );
    }
    else {
        remove_filter( 'page_template', array( 'Raratheme_Companion_Templates', 'rrtc_get_portfolio_template' ) );
    }
    // echo "test";
    // wp_die();
    // $plugin_admin = new Raratheme_Companion_Admin();
    remove_action( 'wp_loaded', array( 'Raratheme_Companion_Templates', 'rrtc_add_portfolio_templates' ) );
}
add_action('plugins_loaded','def',10000);

Any help would be more than appreciable.

Thank you.

  • 1
    Where is $obj = new Raratheme_Companion_Templates; $obj->init(); placed? Inside a called action/another function? If you do global $obj; do you get the created object of class Raratheme_Companion_Templates? – kero Oct 9 '18 at 10:47
  • The class Raratheme_Companion_Templates is from the plugin and yes $obj = new Raratheme_Companion_Templates; $obj->init(); is in the plugin class. And I tried using global $obj but couldn't get it done. – saurav.rox Oct 10 '18 at 6:44
  • Can you post the full file where this code is in? Also check out this and this answer, these will probably be the last resort solution – kero Oct 10 '18 at 9:05
  • I have already added the whole plugin file above. The first block of code with Raratheme_Companion_Templates class is the plugin class. And below is what I tried. I am trying to remove the page template added by plugin class from actions and filters in the child theme. – saurav.rox Oct 10 '18 at 11:04
  • Ok, then you'll have to use a method as describe in the two answers I linked. You cannot use standard methods, because of object orientation and $obj being unavailable (not suprising with such a generic name) – kero Oct 10 '18 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.