Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm making a simple theme framework for personal use and I'm try to set up various options in a class.

Basically, I want to use add_action within my class but the function doesn't seem to be firing (but neither am I getting any errors). I've searched for an answer to this but can't find anything that helped.

What am I doing wrong here?

<?php

class Theme {

private $settings;

public function __construct($settings)
{
    $this->settings = $settings;
    add_action('after_setup_theme', array(&$this, 'boco_set_permalink_structure'));
}

/*
| --------------------------------------------------------------------
| Set Permalink Structure
| --------------------------------------------------------------------
|
| Changes the Wordpress permalink structure.
|
*/

public function boco_set_permalink_structure()
{
    echo 'working...';

    global $wp_rewrite;

    if ($this->settings['change-permalink-structure']):
        $wp_rewrite->set_permalink_structure($this->settings['permalink-structure']);
        flush_rewrite_rules();
    endif;
}

}

I'm instantiating the object like so:

$theme = new Theme($settings);

If I call the boco_set_permalink_structure method manually it works just fine. It just seems that add_action isn't actually doing anything.

Thanks in advance for any help.

EDIT:

As requested in the comments:

The object is instantiated from a file which is included into the functions.php file. $settings is setup as follows:

$settings['change-permalink-structure'] = true;
$settings['permalink-structure'] = '/%postname%/';
$settings['enable-post-thumbnails'] = true;
share|improve this question
1  
Where do you instantiate the object? You have to do that before the hook after_setup_theme is activated. –  birgire Jan 25 at 11:43
    
What data $settings holds when instantiating? –  Rahil Wazir Jan 25 at 12:14
    
Cheers guys, I've added the requested info into the question. –  0Neji Jan 25 at 16:57
    
So you don't see the printed working... debug message? You should consider using a more specific class name, Theme is rather general. The hook after_setup_theme is activated after functions.php so that should be fine. –  birgire Jan 25 at 17:08
    
If I am not mistaken, you are flushing permalinks on every page load. That is very inefficient. –  s_ha_dum Jan 25 at 17:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.