1

I want to create table only if my custom theme is activated. I have tried with after_switch_theme action hook but it is creating table regardless my custom theme. This means it is triggering action if I activate Twenty fifteen or any other theme.

add_action('after_switch_theme', ['\Recruitment\recruitment', 'create_table']);

I want to create table only if my custom theme is a current theme or activating that theme.

Is there any hook for it or any alternate way?

3
  • To be clear - your call to to add_action() is in your custom theme's functions.php (or another file included therein), correct?
    – bosco
    Feb 23, 2017 at 3:36
  • @bosco yes, that's correct. However, my problem is not where to call but which action hook to be used. Can you please help me? Feb 23, 2017 at 3:50
  • Certainly - I was just making sure you weren't calling it in a plugin, since it running as you describe (even when swapping to third-party themes) seems to contradict the Codex's description of the action. It wouldn't be the first time the Codex has been misleading, though
    – bosco
    Feb 23, 2017 at 3:55

1 Answer 1

1

Ah I found it.

You can use after_setup_theme action hook. So it will be like

add_action('after_setup_theme', ['\Recruitment\recruitment', 'create_table']);

Updated Code

$theme = wp_get_theme();

if ( $theme->name == 'My THEME NAME' ) {

  global $wpdb;
  global $charset_collate;

  $sql = "CREATE TABLE IF NOT EXISTS ... $charset_collate;";

  require_once ABSPATH . 'wp-admin/includes/upgrade.php';
  dbDelta( $sql );

  add_option( 'ab_db_version', self::$db_version );

}

// in another file

add_action('after_switch_theme', ['\Recruitment\recruitment', 'create_table']);
8
  • after_setup_theme runs on every page load - if you choose to go this route and use some sort of check to see if the table already exists before creating, keep it mind those database transactions will come with a little overhead.
    – bosco
    Feb 23, 2017 at 3:58
  • @bosco oh yeah, I didn't realize that. Thank you so much for your input. Do you have any other hook? Feb 23, 2017 at 4:00
  • I think you were on the right path the first time, we just need to add a little extra logic :) . One minute
    – bosco
    Feb 23, 2017 at 4:01
  • @bosco I have updated my answer code.. what do you think about it? Feb 23, 2017 at 4:10
  • 1
    You nailed it - your updated code is pretty much what I was going to suggest. The only thing I might change is checking $theme->stylesheet_dir against your theme's programmatically obtained root directory, as theme names could theoretically collide but stylesheet directories really cannot. But I'm going to take a closer look at the the after_switch_theme hook this weekend and see if I can't figure out what's up, seeings as the way the Codex describes it, we shouldn't need either theme check.
    – bosco
    Feb 23, 2017 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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