最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Run a code only on theme activation only during first activation

programmeradmin2浏览0评论
function clearwidgets(){
  //stuff here only runs once, when the theme is activated for the 1st time
}

register_activation_hook(__FILE__, 'clearwidgets');

And the code I am trying to execute is this:

    add_filter( 'sidebars_widgets', 'unset_sidebar_widget' );

function unset_sidebar_widget( $sidebars_widgets ) {
unset( $sidebars_widgets[ '$sidebar_id' ] );
return $sidebars_widgets;
}

That means when the first time the theme is installed it should clear away all the default widget set by WordPress.

Where am I going wrong because the desired result is not achieved? Please suggest me the fix or direct me in the direction so that I can troubleshoot.

function clearwidgets(){
  //stuff here only runs once, when the theme is activated for the 1st time
}

register_activation_hook(__FILE__, 'clearwidgets');

And the code I am trying to execute is this:

    add_filter( 'sidebars_widgets', 'unset_sidebar_widget' );

function unset_sidebar_widget( $sidebars_widgets ) {
unset( $sidebars_widgets[ '$sidebar_id' ] );
return $sidebars_widgets;
}

That means when the first time the theme is installed it should clear away all the default widget set by WordPress.

Where am I going wrong because the desired result is not achieved? Please suggest me the fix or direct me in the direction so that I can troubleshoot.

Share Improve this question asked Jun 24, 2019 at 6:54 WordCentWordCent 1,9066 gold badges34 silver badges60 bronze badges 8
  • are you trying to remove some available default widget options or clear the existing widget instances saved to existing theme sidebars? because the 2nd is unnecessary I am guessing it is the first? – majick Commented Jun 24, 2019 at 7:04
  • register_activation_hook() is only for plugin activation. To handle activation of themes, use the after_switch_theme hook. With regards to widgets, how do you know they're the default? I suggest leaving people's content alone when activating your theme. If you want to show of what your theme can look like, consider using the Starter Content feature: make.wordpress/core/2016/11/30/… – Jacob Peattie Commented Jun 24, 2019 at 7:08
  • No when you install any theme for the first time. WordPress throw its default widgets in the posts sidebar(single posts). I want to get rid of them during theme's first activation. – WordCent Commented Jun 24, 2019 at 7:21
  • Can you suggest final corrected code to achieve what i am trying 2 achieve? – WordCent Commented Jun 24, 2019 at 7:22
  • "how do you know they're the default? " thats why I want to do this only once when the theme is activated for the first time? – WordCent Commented Jun 24, 2019 at 7:28
 |  Show 3 more comments

1 Answer 1

Reset to default 2

What about using WordPress Options API to store a flag whether it is the first switch or not: https://codex.wordpress/Options_API

<?php
  add_action('after_switch_theme', 'setup_theme_options');

  function setup_theme_options () {
    if(get_option('first_theme_activation') === false){
      // Set a flag if the theme activation happened
      add_option('first_theme_activation', true, '', false);

      // stuff here only runs once, when the theme is activated for the 1st time
    }
  }

Going back to Jacob Peattie's note, register_activation_hook() is for plugin use and not for theme switching. And unsetting content could become tricky, because you don't know what is there.

Either way, I hope this answers helps!

发布评论

评论列表(0)

  1. 暂无评论