What about using WordPress Options API to store a flag whether it is the first switch or not:
https://codex.wordpress.org/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!
Related Posts:
- Change the footer text on the login page
- How to remove a metabox from menu editor page?
- Check if page parent has certain template
- Hooking new functions to actions + passing parameters
- Is $hook a global variable in WordPress
- Insert all post IDs in new database table
- Randomise upload filenames (or another solution to hide the original image URL from theft?)
- Handling Body class based on Template
- Woocommerce: hook action/filter I could use to add variation id and price with each attribute opt on WooCommerce Rest api
- Why ajax doesn’t work on certain wordpress hooks?
- Is it possible to intercept all ajax requests and get the parameters and the returns?
- user_profile_update_errors hook not executing
- WordPress Gravatar filter is removing my custom attributes
- Error in custom php function doesn’t exist
- Generating an nonce for Content Security Policy and all scripts – How to make it match/persist for each page load?
- Return a numerical function value in Customizer controls
- Modify custom field input data before creating a new post
- how to make custom ajax handler?
- Omit custom post type from wp-sitemap.xml based on meta key using wp_sitemaps_posts_query_args
- Password minimum length in personal subscription [closed]
- Fatal error: Call to undefined function register_new_user()
- Theme not calling Jquery properly
- WordPress redirect redirecting too many times or not at all
- Why does the post_type_link hook everything twice?
- How to have different site identity logos on each page on Astra Theme [closed]
- How can I hook into the wp_mail function used by BackWPup?
- Get user custom field value on function.php
- Adding function to child theme’s function.php
- Must filter functions receive all arguments passed to them?
- Creating customized php files in theme folder
- Dynamically Generate Functions and Hooks
- How to pass hook variable to function?
- How do I add a function to parent theme
- Contain multiple page templates in one PHP custom template file in WordPress?
- Wp_Schedule_Event every few minutes doesn’t work
- Adding custom field to product category and show it to shop/category shop page
- Custom meta box is not displaying value showing tag as empty
- Discount in the specific product title using keyword ( Woocoommerce )
- Including user data in “new user notification email”
- How do I get the value of a current user’s custom field?
- Help hooking into user_register
- How to auto-generate random numbers in username?
- Hook into all password resets in WordPress and get password before hashing?
- Getting page / post URL on publish and / or update
- Fatal error when using array_diff() function inside of wp_update_nav_menu hook?
- Why transition_post_type hook is called twice for the same post?
- Registration form not registering First and Last name
- WordPress HTML Helper
- Assign new post author IF another user in custom field on post transition
- Create and publish a new post every Monday, listing the post titles and links to the posts published during the previous week
- transition_post_status hook, works – but not if the post is new
- Is there a hook that I can use when a fatal error occurs?
- Displaying Logged-In User Name in WordPress Menu
- Set media upload attachment link to none and hide it in WP v3.5
- Integrating WordPress to my website, while keeping my own authentication system
- How to add default images into theme customizer image control?
- Successful or Error Message after running mysql code in functions.php
- How can one utilize a variable as a callback function name for add_settings_field
- Custom Logo URL | Help me print the URL of the custom logo I inserted into my theme
- How to restrict page access to logged-in users?
- How to load a template-part based on a url wildcard?
- Adding Default Settings to Theme My Login plugin
- function to assign user role based on a field from usermeta
- Set meta field to publish date + 2 weeks
- Incorrect amount of posts returned when filtering related Woo products by custom taxonomy
- Copyright info change – Corporate Plus Theme PHP [closed]
- How to list posts from a plugin taxonomy?
- How to remove ?p= from wordpress short links
- How to make function appear in sentence?
- How to define a variable already defined in a plugin function?
- woocommerce specific quantities for product
- My website is not showing Footer section
- Custom query vars filters problem with pagination
- How to Display a List of Child Pages For a Parent Page in WordPress
- Displaying Slider in Front Page (home) using Customizr Theme
- How to add PHP pagination to wordpress
- Taxonomy term description with style, only if the description exists?
- Restrict post to current user in postmeta
- copy fields value to another field
- posts_per_page displays only 2 posts instead of 4 posts
- Get parent category name when only child category is applied to a product
- Can an AJAX callback function access a PHP variable which was defined during initial page load?
- Adding new divs with custom classes to products on WooCommerce shop page [closed]
- Adding product permalink on admin order page throwing error
- Before & After Content – After Content directly below Before Content when using require_once
- Attaching an upload to a user
- How fix error in the WordPress loop?
- use system script advertising in wordpress multisite
- Use WP_Query Data In Cookie
- Extend the WP_Customize_Image_Control class to change its non-frame $button_labels
- Help with my first Metabox helper class
- ISOTOPE – Missing/Invalid Arguement Get Terms
- How to add custom checkout field in user details mail template
- Pagination not displaying
- Setting WooCommerce currency programmatically
- Masking external links with internal link for member-only
- Why a class property becomes empty?
- Hide category by tag
- understanding wp_next_scheduled
- add_rewrite_rule image from /images/site2/favicon.ico to /favico.ico