简体   繁体   中英

Wordpress How to change plugin settings in Theme function.php

I'm new to Wordpress and working on my first theme. The theme will be heavily combined with one plugin. I'm wondering how I could change some of the plugin settings in the theme function.php without touching the plugin itself.

I have tried looking it up on the internet but haven't found any concrete answers for my issue.

What I'm using:

Wordpress 4.2 , Justimmo api plugin

The Problem:

I'm trying to figure out how I could in my theme function.php redirect/replace the plugin template files with the ones that are in my theme.

Currently in the plugin folder are template files and how they will render on the website. I would write my own template file, making sure it looks like my theme and replace it with the plugin template file.

If I were to edit the template files directly in the plugin folder, whenever there might be a update, they would be overwritten. I don't feel that is right.

After looking around the internet, I feel I could achieve it with add_filter , but not fully sure how to.

The Plugin

On Github: https://github.com/justimmo/wordpress-plugin/blob/master/justimmo.php

In the original plugin has a lines (row 240 for indexPage() and row 328 for getIndexUrl() ):

class JiApiWpPlugin 
{

    function indexPage()
    {
        include(JI_API_WP_PLUGIN_DIR . '/templates/index.php');
    }
    function getIndexUrl()
    {
        return $this->getUrlPrefix().'ji_plugin=search';
    }
}

It dictates where one of the template files can be found, and also gives a permalink to the plugin.

What I want

I would like to add a filter to take the template file from my theme instead, and to rewrite the path to the template file.

If there is an easy way for adding a hook, that overwrites it, great. Also I have found that I could do some template replacements with add_filter .

Have tried:

add_filter( 'template_include', 'gulz_justimmo_page_template' );
function gulz_justimmo_page_template( $page_template )
{
    if ( is_page( 'ji_plugin' ) ) {
        $page_template = dirname( __FILE__ ) . '/justimmotemp/justimmo-index.php';
    }
    return $page_template;
}

But I'm not fully sure how to check if the plugin permalinks page is activated.

I feel it should be a simple thing for users overwrite or redirect plugin template files with the ones that are in their theme, but I can't figure it out.

I would be grateful for all help and advice.

Unfortunately the function indexPage() is called by another function which is then hooked by reference.....

add_action('template_redirect', array(&$this, 'templateRedirect'));

What you need to do is remove this function and replace with your own custom function (copy the code from the plugin and modify to call a custom function for that page). The problem is you can't use remove_action because no name was passed with add_action so wp creates one that changes with every load.

So we need a function to find the function added:

function remove_anonymous_action( $name, $class, $method ){
        $actions = $GLOBALS['wp_filter'][ $name];

        if ( empty ( $actions ) ){
            return;
        }

        foreach ( $actions as $prity => $action ){
            foreach ( $action as $identifier => $function ){
                if ( is_array( $function) && is_a( $function['function'][0], $class ) && $method === $function['function'][1]){
                    remove_action($tag, array ( $function['function'][0], $method ), $prity);
                }
            }
        }
}

What you can do then do is call this after the action has been added above using the priority argument (this removes the function for all pages inside the function btw)

 // the action adding the action is added in parse_query filter...use this as the point to remove the added action
 add_action('parse_query', 'remove_plug_action', 50);

 function remove_plug_action(){
  // call our function with the name of the hook, classname and function name
         remove_anonymous_action('template_redirect','JiApiWpPlugin','templateRedirect');

 //add a custom function to replace the one we removed.
 add_action('template_redirect', 'customtemplateRedirect');
}

Modify the function below to call your theme based file.

function customtemplateRedirect(){
    global $wp;
    global $wp_query;
    if (get_query_var('ji_plugin') !== '') {
        switch (get_query_var('ji_plugin'))
        {
            case 'property':
                $this->propertyPage();
                exit;
                break;
            case 'expose':
                $this->exposeDownload();
                exit;
                break;
            default:
                $this->indexPage();
                exit;
                break;
        }
    }
}

also you can check if the hook has been removed using

 $hook_name = 'template_redirect';
 global $wp_filter;
 var_dump( $wp_filter[$hook_name] );

Anon filters will have a long key eg 12i90rkl2rkljeri (its random every time). Break down the process by just removing the add action in remove_plug_action() and see what you get. The action should be removed if not var dump $_GLOBALS['wp_filter']['template_redirect'] to see what it looks like. It might take a fit of fiddling around. You also need to check your if statements (var_dump values being fed to it to check if they will pass or fail etc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM