0

I'm creating a plugin (I'm very newbie in this) to load custom taxonomies, custom post types and an automatic activation of a theme, so I have this code:

// Custom Post Types and Taxonomies
require_once('inc/cpt-taxonomies.php');

// Switch Theme
function updateTheme($theme){
    update_option('template', $theme);
    update_option('stylesheet', $theme);
    update_option('current_theme', $theme);
}
updateTheme('Creative_Grid');

require_once('inc/initial-setup-theme.php');

The issue is that when is activated I can't swith to other theme in Appereance Page, it's like the plugin reactivate it when a try to switch to other theme. I can confirm that when I switched to another theme all the code of the plugin run again, and I dont want that, I want to execute dthe code when is activated.

Some idea? Other question, what kind of php conditional should I have to prevent a failure if that theme is not available?

Thanks, Roy

2
  • When you run the code, on which hook? Maybe you have no hook, is it a plugin then runs always to a default loading and run always and set the theme values inside the settings.
    – bueltge
    Dec 20, 2014 at 23:09
  • Hi, I use to utilize some code in functions.php, and this is my first time with plugins. I realize myself I dont have some basic understanding on how plugins works. Yes, I was not using hooks. Below @cybmeta pointed out this question. And I realize how to solve this particular question, but this plugin has other functionalities and as said, custom post types have to use a different hook. I have other pieces of code I want to run in this plugin (it's a plugin for setting up theme functionalities), and I'm trying to find out how to make work all together... I accept recommendations
    – Dedalos01
    Dec 21, 2014 at 0:32

1 Answer 1

0

It seems that you are executing updateTheme() in every page load. To execute tasks only on activation you should hooke the updateTheme() function to the register_activation_hook action hook:

register_activation_hook( __FILE__, 'activation_callback' );
function activation_callback() {
     //The code inside this function is executed only on plugin activation
     $theme = 'twentyeleven'; //SET the value of $theme to whatever you want (not provided in the question)
     updateTheme($theme);
}

// Switch Theme
function updateTheme($theme){
    update_option('template', $theme);
    update_option('stylesheet', $theme);
    update_option('current_theme', $theme);
}
7
  • Awesome, thanks for helping. I dind't know that by default a plugin is executed every time page is loaded, and I dont know about make it work on actvation also. I need run more code only on activation, I suppouse the way to do it is execute the functions in side activation_callback(), no?
    – Dedalos01
    Dec 20, 2014 at 21:23
  • Yes, it is the correct way.
    – cybmeta
    Dec 20, 2014 at 21:24
  • ok..., I tried but not working now. The CPTypes are not being registered, could you check out my code here adn tell me what Im doing wrong? pastebin.com/embed_iframe.php?i=8fpHs7bx
    – Dedalos01
    Dec 20, 2014 at 21:36
  • register custom post types should be done on init action hook. See register_post_type documentation where it says: "It will not work if called before 'init', and aspects of the newly created or modified post type will work incorrectly if called later. "
    – cybmeta
    Dec 20, 2014 at 21:47
  • so, for understanding... if it's loaded by 'init' the cpt are not registered every time I load the page. Sorry (for my ignorance) for the question I thought everything run everytime a page is loaded by default.
    – Dedalos01
    Dec 20, 2014 at 21:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.