Write Own Code to Add Category and Tags into WordPress Page

  • Last updated: September 13, 2024 By Sunil Shaw

How to Write Own Code to Add Wordpress Page into Category and Tags

Introduction

Heyy there, i am not talking about adding plugins to your wordpress site. Probably there is tons of plugins available in wordpress plugin store, But today we will learn how to write own code to add functionality to add category and tags into wordpress page. We will write our own code to add it in your theme functions.php file.

What is WordPress, its Features, Benefits, Advantages and Disadvantage

WordPress is an Web CMS (Content Management System), written in PHP, created for posting blog content but after being popular it evolves and start supporting mail system, Online ecommerce, learning management system, media gallery, internet forums and community.

Many time before wordpress is only a tool to create blog. After some time wordpress core code evolves, it’s change the market share because supports theme and plugins to add more functionality. Now you can create any type of website with wordpress themes and plugins. For example, a wordpress theme WooCommerce that converts entire wordpress into ecommerce website.

  • Bussiness Websites
  • Ecommerce Website
  • Blog
  • Resume
  • Portifolio
  • Forum
  • Social Networking
  • Membership sites.

You can create any kind of website, you dreamed.

Who Uses WordPress

Now these day wordpress is used by many websites, 60% percent world widely usage. Lot’s of bussiness and professional bussiness and company uses wordpress for serving their content.

Microsoft

Who Uses WordPress - codewithronny

American multinational corporation and technology company Microsoft blog is power by wordpress. Microsoft also use wordpress for another product.

Sony

Sony uses wordpress - codewithronny

Sony music company is one of the biggest music label. they publishing world no1, music in the world and proud itself.

The Walt Disney Company

the walt disney company uses wordpress - codewithronny

The walt disney company write about character, and other product details on wordpress power blog.

Here i am going to categories wordpress into only three category.

  1. WordPress Core
  2. Theme and frontend part
  3. Plugins and software parts

Add Colors In WordPress Theme Support Editor Color Palette

  • Benefits and Advantages: Writting posts on wordpress based website is very easy. WordPress have user friendly dashboard to write, edit, update and publish posts. There is lots of themes and plugins available to customize design and layouts, plugins add more and more features to wordpress websites. WordPress have large and open community to support, update resources and shares knowledge regularly. Most important thing to know wordpress are inheritly SEO friendly. You can develop your own theme and plugins using wordpress documents. By using your own theme you can structure and design your own web page.
  • Disadvantages: WordPress widely used more than other CMS, that’s why attacker can attack, security can be in risk. By not using professionally coded theme and plugins it can slow down your websites. Using plugins more for functionality can create dependencies on plugins, if plugins get older, updates and maintance can be more hard and time consuming. You cann’t change or edit wordpress core for adding new features.

Write Own Code to Add Category and Tags into WordPress Web Page

Lets write own code to register categories and tags to your WordPress pages, extending their functionality beyond the posts. Let’s breakdown this below code.

  1. We have to register a function name register_category_and_tag_with_pages.
  2. We will use taxonomy object to register category and tag, that are used for posts.
  3. Lets’ hook this register_category_and_tag_with_pages function to Wordpres init.

Before writing this code, you can observe that your page area looks like the image below.

Write Own Code to Add Category and Tags into WordPress Page

Register a Function

Lets open your wordpress website dashboard go to appereance and then theme file editor and edit functions.php. Then create a custom function in your WordPress theme’s functions.php file. This function is responsible for registering category and tags to your WordPress all pages.

function register_category_and_tag_with_pages(){
  // write your code here
}

register category taxomony object for page. By default WordPress use this toxomony to register posts under category object. This register_taxonomy_for_object_type code allow pages to be categorized like posts.

register_taxonomy_for_object_type('category', 'page');

register object tags taxomony for WordPress pages. This register_taxonomy_for_object_type WordPress function is used by default for posts. This register_taxonomy_for_object_type code allow tags to be added in pages.

 register_taxonomy_for_object_type('post_tag', 'page');

Now hooks the register_category_and_tag_with_pages function to WordPress init action. When WordPress core initializes, this add_action an wordpress function call the function and ensures that tags and categories are registered for pages.

add_action( 'init', 'register_category_and_tag_with_pages');

Here is the full function. Just copy and paste into your theme’s functions.php file. If your websites work fines congratulations your code working.

function register_category_and_tag_with_pages(){
  /*add categories and tags to pages*/
  register_taxonomy_for_object_type('category', 'page');
  register_taxonomy_for_object_type('post_tag', 'page');
}
add_action( 'init', 'register_category_and_tag_with_pages');

Here is the result:

Write Own Code to Add Category and Tags into WordPress Page

Modify WordPress query before getting executed

Let’s create one more custom PHP function named register_pre_get_category_and_tag_with_pages, hook this function to pre_get_posts action. We will pass $query as a parameter to load main query in our custom function then we will write some conditions.

function register_pre_get_category_and_tag_with_pages( $query ) {
// write your code here
}
add_action( 'pre_get_posts', 'register_pre_get_category_and_tag_with_pages');

Let’s write code to check if requests is coming from WordPress admin area or if it’s not the main query. If conditions are true this function returns without any changes, ensuring our custom code modify only frontend not the main query.

if ( is_admin() || ! $query->is_main_query() ) {
    return;
  }

We are going to write if statement where both conditions return true our code will executes. Check if current query($query->is_main_query()) is for category page archive and query is main query. this will set post_type to post and page.

if($query->is_category && $query->is_main_query()){
    $query->set('post_type', array( 'post', 'page'));
  }

Write one more if condition, if current query is for tag archive page, and query is main query, both return true then set post_type to posts and pages.

if($query->is_tag && $query->is_main_query()){
    $query->set('post_type', array( 'post', 'page'));
  }

Here is the full code snippet, just copy and paste into your functions.php file , save it. If your websites working currectly, congratulations you added page to category and tags.

function register_pre_get_category_and_tag_with_pages( $query ) {

  if ( is_admin() || ! $query->is_main_query() ) {
    return;
  }
  /*view categories and tags archive pages */
  if($query->is_category && $query->is_main_query()){
    $query->set('post_type', array( 'post', 'page'));
  }
  if($query->is_tag && $query->is_main_query()){
    $query->set('post_type', array( 'post', 'page'));
  }
}
add_action( 'pre_get_posts', 'register_pre_get_category_and_tag_with_pages');

Here you can see both function and full code snippet.

Note: I tested this code on PHP Version:- 8.3.3 and WordPress Version:- 6.3.3

function register_category_and_tag_with_pages(){
  /*add categories and tags to pages*/
  register_taxonomy_for_object_type('category', 'page');
  register_taxonomy_for_object_type('post_tag', 'page');
}
add_action( 'init', 'register_category_and_tag_with_pages');

function register_pre_get_category_and_tag_with_pages( $query ) {

  if ( is_admin() || ! $query->is_main_query() ) {
    return;
  }
  /*view categories and tags archive pages */
  if($query->is_category && $query->is_main_query()){
    $query->set('post_type', array( 'post', 'page'));
  }
  if($query->is_tag && $query->is_main_query()){
    $query->set('post_type', array( 'post', 'page'));
  }
}
add_action( 'pre_get_posts', 'register_pre_get_category_and_tag_with_pages');

Now you can add your page into any category. You can also add tags to them.

Write Own Code to Add Category into WordPress Page
Write Own Code to Add Tag into WordPress Page


Follow on:
  • Twitter
  • Facebook
  • Instagram
  • Linkedin
  • Linkedin
Sunil Shaw

Sunil Shaw

  • Youtube
  • Instagram
  • Twitter
  • Facebook
About Author

I am a Web Developer, Love to write code and explain in brief. I Worked on several projects and completed in no time.

Related Articles

Popular Language Tutorials

If You want to build your own site Contact Us