0

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.

Community
  • 1
  • 1
olkr
  • 41
  • 6

1 Answers1

0

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 e.g. 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)

David
  • 5,897
  • 3
  • 24
  • 43
  • btw dont forget to click the tick mark beside the answer that solved the issue for you (i note the last q you posted was not accepted?) – David Aug 30 '15 at 00:20
  • Hei David. Thank you for your answer. I have a question as I feel there are some steps Im not fully familiar with and that prevents everything to work. In the beginning you said: { replace with your own custom function (copy the code from the plugin and modify to call a custom function for that page). } What I did was copied the parseQuery function from the plugin into my function.php file. You can see the current file: http://paste.ofcode.org/hHTC5jV8swa9g4Zkcs9rp3 Also added all the code that you shared with me. – olkr Aug 30 '15 at 17:10
  • Feel there needs to be something to tell to my theme to take that code from that justimmo plugin. To clue it together. Right now I don't feel they are connected as there are no changes. – olkr Aug 30 '15 at 17:10
  • You see wordpress uses hooks and filters for this purpose. This allows you to modify processes that are contained elsewhere. You could just modify the plugin but then when its updated, you lose the changes you made. Extending the class wouldnt work here as your new class would not be called so you need to unhook what you dont want and add what you do if that makes sense? I've added a bit on above for you to check. – David Aug 30 '15 at 19:13
  • Hei David, sorry for the long delay. Haven't forget it. Just haven't managed to do it yet, as I'm quit busy with other projects that deadline is approaching. Will look it over in detail again in couple of days and if works, will add it as resolved. – olkr Sep 02 '15 at 08:02