Home PHP JavaScript CSS WordPress APIs .htaccess Other How-To Useful Scripts What I Recommend
Posted on by Aleksandar Gichevski ()
When creating theme for WordPress in most of cases you need to execute bunch of code in order to achieve what you want and integrate fully your theme. What you can do is
  • create new categories within theme
  • create predefined pages
  • create new records in database
  • modify admin panel and user interface
  • and lot more...
There are 2 ways how this can be done and both ways are different...

Execute code everytime page loads

First one is right away after activating theme but this involves executing code on every page load, which means with every refresh that code is executed. In below example I remove 2 features that I don't want to be used in my theme and those are: 1. Users to be able to add custom background WP Reference 2. Users to be able to add custom header WP Reference So because in my theme they will be able to set custom header and background I want to disable those options from WP Admin.
add_action( 'after_setup_theme','remove_options', 100 );
function remove_options() {
	remove_custom_background();
	remove_custom_image_header();
}
So what we do first is creating function 'remove_options' where we add the predefined wordpress function for disabling these options. After that we just add_action hook where we point that after setting up theme execute those commands.

Execute code only once after activating theme

So here comes main and important part where you need to execute code only once when theme is activated and never again. I use this to create records in database, add categories, pages everything that is needed to integrate my theme. It is weird because there is not predefined function or hook from wordpress that you can use for this, so we must create our code that will do that. I found a very simple solution which works for me in any theme and any wordpress version.
 if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
 
 wp_insert_term('News', 'category');
  wp_insert_term('Business', 'category');
  wp_insert_term('Gaming', 'category');
  wp_insert_term('Entertainment', 'category');
  wp_insert_term('Lifestyle', 'category');
  
  }
In above code after setting up code we insert 5 categories into WordPress. So mainly what we do is check is it theme page, does it exist activated parameter and are we in admin. If all these conditions are fulfilled then that code will execute.

Other Posts You Might Like

Get a fast, free website audit


Enter your URL below to get full in-depth SEO report and tips.

‚Äč

Useful Scripts

Most Popular Posts

Recent Posts

June 14th, 2014
Website Optimization Using Gzip Compression

June 10th, 2014
Google Search AutoComplete API

May 14th, 2014
What is a Tag Cloud and How to Calculate it by Formula

March 27th, 2014
Error writing file /tmp (errcode 28) Solved!

February 15th, 2014
Fixed CSS & HTML Navigation Bar

February 9th, 2014
Benchmark Your Server (CPU, File IO, MySQL) with SysBench

February 8th, 2014
JavaScript Array Basics

January 29th, 2014
JVZoo IPN API in PHP

January 25th, 2014
Payoneer Debit Card for Freelancers to receive money online

January 24th, 2014
How HTML Color Codes are generated?

Read Latest Posts directly on Facebook

Archive