{"id":558,"date":"2022-09-15T14:00:33","date_gmt":"2022-09-15T14:00:33","guid":{"rendered":"https:\/\/demo.wpartisan.net\/developmentsite\/seo-meta-tags-in-wordpress-without-plugin\/"},"modified":"2024-11-04T03:24:07","modified_gmt":"2024-11-04T03:24:07","slug":"seo-meta-tags-in-wordpress-without-plugin","status":"publish","type":"quick-tips","link":"https:\/\/demo.wpartisan.net\/developmentsite\/seo-meta-tags-in-wordpress-without-plugin\/","title":{"rendered":"Add Meta Tags in WordPress Without Plugin for SEO"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">First, we need to know what is meta tags and why it is important to add them to pages, posts or categories. In SEO, meta tags play a key role for better ranking on search result pages. It provides concise information to search engine spiders to understand the meaning (context) of your content. It will be shown on the search result pages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example of the meta description tag on search results:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"410\" src=\"https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/09\/meta-description-3-1536x615-1-1024x410.png\" alt=\"\" class=\"wp-image-2259\" srcset=\"https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/09\/meta-description-3-1536x615-1-1024x410.png 1024w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/09\/meta-description-3-1536x615-1-300x120.png 300w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/09\/meta-description-3-1536x615-1-768x308.png 768w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/09\/meta-description-3-1536x615-1.png 1536w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>How to add meta tags to a WordPress website without plugins?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the theme functions.php file, first, you need to add meta fields for posts, pages or custom post types to add meta keywords and meta descriptions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the code to add meta fields and save meta values: <\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">function func_add_custom_box() {\n\t\n\t$post_types = array('post', 'page', 'quick-tips', 'programming_with_php');\n\t\n\tforeach ( $post_types as $post_type ) {\n\t\tadd_meta_box(\n\t\t\t'wa_seo_metas',\n\t\t\t'SEO Meta BOXES',\n\t\t\t'wa_seo_meta_box_html',\n\t\t\t$post_type\n\t\t);\n\t}\n}\nadd_action( 'add_meta_boxes', 'func_add_custom_box' );\n\nfunction wa_seo_meta_box_html( $post ) {\n\t\n\techo '&lt;p class=\"meta-options\">\n\t\t&lt;label for=\"wa_meta_keywords_field\">Meta Keywords&lt;\/label>\n\t\t&lt;input id=\"wa_meta_keywords\" class=\"widefat form-row-wide\" type=\"text\" name=\"wa_meta_keywords\" value=\"'.esc_attr( get_post_meta( get_the_ID(), 'wa_meta_keywords', true ) ).'\">\n\t&lt;\/p>\n\t&lt;p class=\"meta-options\">\n\t\t&lt;label for=\"wa_meta_description_field\">Meta Description&lt;\/label>\n\t\t&lt;textarea id=\"wa_meta_description\" class=\"widefat form-row-wide\" name=\"wa_meta_description\">'.esc_html( get_post_meta( get_the_ID(), 'wa_meta_description', true ) ).'&lt;\/textarea>\n\t&lt;\/p>';\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">function wa_insert_submission( $post_id ) {\n\n\t$wa_meta_keywords = sanitize_text_field( $_POST['wa_meta_keywords'] );\n\t$wa_meta_description = sanitize_textarea_field( $_POST['wa_meta_description'] );\n\t\n\tif ( !empty( $wa_meta_keywords ) ) {\n\t\tupdate_post_meta( $post_id, 'wa_meta_keywords', $wa_meta_keywords );\n\t}\n\t\n\tif ( !empty( $wa_meta_description ) ) {\n\t\tupdate_post_meta( $post_id, 'wa_meta_description', $wa_meta_description );\n\t}\n\t\n}\nadd_action( 'save_post', 'wa_insert_submission' );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the output of the above code :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large \\&quot;wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"240\" src=\"https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/09\/seo-meta-boxes-1-1024x240.png\" alt=\"\" class=\"wp-image-2267\" srcset=\"https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/09\/seo-meta-boxes-1-1024x240.png 1024w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/09\/seo-meta-boxes-1-300x70.png 300w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/09\/seo-meta-boxes-1-768x180.png 768w, https:\/\/demo.wpartisan.net\/developmentsite\/wp-content\/uploads\/2022\/09\/seo-meta-boxes-1.png 1476w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We need to add that meta information in the frontend using &#8216;wp_head&#8217; action hook. Please check the code below :<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">function wpartisan_add_seo_metas() {\n    global $post;\n    if ( is_singular() ) {\n\t\t\n\t\t$wa_meta_keywords = trim(get_post_meta($post->ID,'wa_meta_keywords',true));\n\t\t$wa_meta_description = trim(get_post_meta($post->ID,'wa_meta_description',true));\n\n\t\tif( !empty( $wa_meta_description ) ){\n\t\t\t$des_post = $wa_meta_description;\n\t\t}else{\n\t\t\t$get_short_des = $post->post_excerpt;\n\t\t\tif( empty( $get_short_des ) ){\n\t\t\t\t$get_short_des = $post->post_content;\n\t\t\t}\n\t\t\t$des_post = strip_tags( $get_short_des );\n\t\t\t$des_post = strip_shortcodes( $des_post );\n\t\t\t$des_post = str_replace('\"', '', $des_post);\n\t\t\t$des_post = str_replace( array(\"\\n\", \"\\r\", \"\\t\"), ' ', $des_post );\n\t\t\t$des_post = mb_substr( $des_post, 0, 145, 'utf8' );\n\t\t}\n\t\t\n        echo '&lt;meta name=\"description\" content=\"' . $des_post . '\" \/>' . \"\\n\";\n\t\t\n\t\tif( !empty( $wa_meta_keywords ) ) {\n\t\t\techo '&lt;meta name=\"keywords\" content=\"' . $wa_meta_keywords . '\" \/>' . \"\\n\";\n\t\t}else{\n\t\t\tif($post->post_type == 'post') {\n\t\t\n\t\t\t\t$cats = get_the_category( $post->ID );\n\t\t\t\t$tags = get_the_tags( $post->ID );\n\t\t\t\n\t\t\t\tif( !empty( $cats ) &amp;&amp; !empty( $tags ) ){\n\t\t\t\t\t$keywords = array_merge($cats,$tags);\n\t\t\t\t}else{\n\t\t\t\t\tif( !empty( $cats ) )\n\t\t\t\t\t\t$keywords = $cats;\n\t\t\t\t\tif( !empty( $tags ) )\n\t\t\t\t\t\t$keywords = $tags;\n\t\t\t\t}\n\t\t\t\n\t\t\t}elseif($post->post_type == 'quick-tips'){\n\t\t\t\t$keywords = get_the_terms( $post->ID, 'topics' );\n\t\t\t}\n\t\t\t\n\t\t\t$metakeywords = '';\n\t\t\tif( !empty( $keywords ) ){\n\t\t\t\t$metakeywords = join(', ', wp_list_pluck($keywords, 'name'));\n\t\t\t\techo '&lt;meta name=\"keywords\" content=\"' . $metakeywords . '\" \/>' . \"\\n\";\n\t\t\t}\n\t\t}\n\t\t\n    }\n    if ( is_home() ) {\n        echo '&lt;meta name=\"description\" content=\"This website teach you how to use WordPress Actions And Filters Hook, Develop Plugins and Themes And how to do programming with PHP. Also we offer a simple and top notch solution for your complexity in PHP, WordPress and WooCommerce.\" \/>' . \"\\n\";\n        echo '&lt;meta name=\"keywords\" content=\"WordPress, WordPress Blog, WordPress Article, WooCommerce, Actions And Filters Hook, Plugins, Themes, Programming with PHP, Blog\" \/>' . \"\\n\";\n    }\n    if ( is_category() ) {\n        $des_cat = strip_tags(category_description());\n\t\t$des_cat = mb_substr( $des_cat, 0, 145, \\'utf8\\' );\n        echo '&lt;meta name=\"description\" content=\"' . $des_cat . '\" \/>' . \"\\n\";\n    }\n}\nadd_action( 'wp_head', 'wpartisan_add_seo_metas');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above code, you can see I added some conditions to add meta description dynamically and meta keywords from post or page content, categories and tags when meta keywords and description field are empty.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SEO, meta tags play a key role for better ranking on search result pages. It provides concise information to search engine spiders to understand the meaning (context) of your content.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}}},"tags":[50,17],"topics":[49,45],"class_list":["post-558","quick-tips","type-quick-tips","status-publish","hentry","tag-meta-tags","tag-seo","topics-php","topics-wordpress"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"WPArtisan","author_link":"https:\/\/demo.wpartisan.net\/developmentsite\/author\/developmentsiteauth\/"},"uagb_comment_info":0,"uagb_excerpt":"In SEO, meta tags play a key role for better ranking on search result pages. It provides concise information to search engine spiders to understand the meaning (context) of your content.","_links":{"self":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/quick-tips\/558","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/quick-tips"}],"about":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/types\/quick-tips"}],"author":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/comments?post=558"}],"wp:attachment":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media?parent=558"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/tags?post=558"},{"taxonomy":"topics","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/topics?post=558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}