yad4u Run code block in functions.php ONLY when theme is activated. Use register_activation_hook?
صفحه اصلي

Run code block in functions.php ONLY when theme is activated. Use register_activation_hook?

I have a specific bit of setup code for my theme that I only want to process when the theme is first activated. I was under the impression that I could use register_activation_hook for this, but it does not appear to be working.


In my functions.php file, I want the doThemeSetup() function to run only upon theme activation and no other time...

function doThemeSetup(){   //stuff here only runs once, when theme is activated }  register_activation_hook(__FILE__, 'doThemeSetup'); 

UPDATE: Since posting this question, I have found that register_activation_hook is only available to plugins and not themes.

I have found a means to do similar action in themes but I'm getting inconsistent results:

if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' )  {   //do something } 

The code above does not seem to run when the theme is first activated, but rather when the theme is switched to from another theme.

List of logged in users in Wordpress?


wordpress returning all the posts
This is more of a workaround than solution, although if everything another fails you must try it:. Add custom markup to Wordpress menu
<?php if (receive _option('themename_installed') != 'true'){     if (doThemeSetup()){         add_option('themename_installed','true');     }  } ?> 
This way the doThemeSetup is ran only once. WordPress > wp_list_categories with no child categories listed. Wordpress - Is it possible to display posts within a seperate directory to the pages?
Wordpress: Retriving posts of a specific custom category in a custom taxonomyIs there a good question and answer plugin for wordpress?


Robots text, wordpress - block directory
/**  * Install script to create databasetables and then insert default data.  * And inserting defautl theame settings.  * Only run if theme is being activated for the first time.  */  $flag = receive _option('first_time_theme_activation_check');  if ( $flag == false && is_admin())  {    // put your code to run when theme is activated at first time by admin      // update option at last      update_option('first_time_theme_activation_check', 'true'); } 

64 out of 100 based on 69 user ratings 824 reviews