I have a fully working code to collect the data of 2 meta fields (title and description) on all pages in case the auto generated (or empty) meta is not suitable. It works on every page exact 2:
– The WordPress blog overview (archive)
– The WooCommerce shop page (also archive page)
The 2 ACF fields within the pages look like to be totally ignored.
Any reason for that? And how to overwrite this behavior?
I understand that both pages are like kinda system pages where I can’t directly edit the content but I do want to edit the generated (or empty) meta data.
Here’s the working code (except for the 2 archive pages):
// Add social media friendly meta tags
function yl_add_meta_tags_to_wp_head() {
setup_postdata( $post );
$custom_meta_title = get_field('yl_meta_title' );
$custom_meta_description = get_field( 'yl_meta_description' );
if ($custom_meta_title) {
$meta_title = $custom_meta_title;
} else {
$meta_title = get_the_title();
}
if ($custom_meta_description) {
$meta_description = $custom_meta_description;
} else {
$meta_description = substr(wp_strip_all_tags(get_the_excerpt()),0,150) . '...';
}
echo '
<meta name="meta_name" content="' . $meta_title . '" />
<meta name="description" content="' . $meta_description . '" />
';
}
add_action( 'wp_head', 'yl_add_meta_tags_to_wp_head', 1, 1 );
EDIT:
Let me be more clear: Why are the ACF fields in my shop page (backend) filled with data but complete ignored in the frontend source code...? (This behavior only happens on shop and archive pages).
i want it to be
I want to replace the top Store page name on the wordpress product page with the product name, can anyone help?
example:
i want it to be
How can I do? There is no such setting in the properties of the theme?
Not knowing what theme you are using or how the Shop title is being generated, try adding this code to your functions.php file
add_filter( 'woocommerce_page_title', 'woo_shop_page_title');
function woo_shop_page_title( $page_title ) {
if( 'Shop' == $page_title) {
return the_title();
}
}
This code replaces the page title Shop with the title of the product (post).
I found a code snippet to display the content before the main content and it worked.
Currently the content is displayed on all pages. (except shop page)
The code :
add_action( 'woocommerce_before_main_content', 'BannerShop', 35 );
function BannerShop(){
if(!is_shop()){
echo '<img src="https://localhost/demosite/wp-content/uploads/2015/06/512x356.png" >';
}
}
What I want to ask is, how to display content only for color attribute products in the form of links.
Example :
The display (content) will ONLY SHOW when the url is like this :
mysite.com/color/red/
Sorry if the explanation is not good because I don't really understand this.
Any help is greatly appreciated.
thank you
I understand your question is about displaying that extra content, if the current query is for a product archive page only showing products of a certain attribute 'color'.
Each WooCommerce attribute is an independent taxonomy.
WordPress's is_tax('parameter') function checks, if the query is for an existing custom taxonomy archive page (other than category & tag) & if the query is for that specific taxonomy 'parameter', in your case 'color'.
So, this code snippet in your functions.php or equivalent plugin should work:
add_action( 'woocommerce_before_main_content', 'BannerShop', 35 );
function BannerShop(){
(is_tax('color')) {
echo '<img src="https://localhost/demosite/wp-content/uploads/2015/06/512x356.png" >';
}
}
Though, to make the above template WooCommerce override work, declare WooCommerce support for your theme by adding the following lines to your functions.php or plugin equivalent:
function theme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'theme_add_woocommerce_support' );
So, I know you can put:
function page_title_sc( ){
return get_the_title();
}
add_shortcode( 'page_title', 'page_title_sc' );
Into function.php to fetch the page title and get a [page_title] shortcode within Wordpress, but is it possible to do exactly that for keywords?
I put a focus keyword down in Yoast SEO and I would like to have a shortcode for that.
Or another idea: is there a way to have a custom shortcode field in every page? So that I only have to put something into that once and can use it as a shortcode within the whole page?
If you want a shortcode for Yoast to output the keyphrase automatically:
function wpso_61018203_output_yoast_keyphrase() {
// Make sure Yoast is installed/active.
if ( class_exists( 'WPSEO_Meta' ) ) :
// Hold the global post object.
global $post;
return WPSEO_Meta::get_value( 'focuskw', $post->ID );
endif;
}
add_shortcode('yoast_kw', 'wpso_61018203_output_yoast_keyphrase' );
You can then do this ['yoast_kw'] in your content, or use echo do_shortcode('[yoast_kw]'); in your template.
Use get_post_meta and grab _yoast_wpseo_focuskw:
function page_focus_keyword( ){
return get_post_meta(get_the_ID(), '_yoast_wpseo_focuskw');
}
add_shortcode( 'focus_keyword', 'page_focus_keyword' );
WordPress filters all content to make sure that no one uses posts and page content to insert malicious code in the database. This means that you can write basic HTML in your posts, but you cannot write PHP code.
And you want to use Wordpress shortcode for focus keyword.
Possible solution given on Wordpress.org Yoast SEO plugin support query on similar query, and checkout the linked solution
insert the below php snippet in functions.php
if(!function_exists('wpseoFocusKW'))
{
function wpseoFocusKW()
{
$focuskw = wpseo_get_value('focuskw', $post->ID);
echo $focuskw;
}
}
And to use the shortcode in another page for focused keyword, insert the below shortcode in any pages with yoast-seo plugin :
Shortcode for focus keyword
[<?php wpseoFocusKW();?>]
I would like to remove the category & tag base from WordPress URL. I have come across other posts and solutions which used plugins. I would like to stay away from plugins and have a solution from within functions.php. This would prevent any future plugin updates or WordPress default files from being changed.
Any help would be appreciated. Thanks!
I have tried these solutions so far:
This htaccess solution did not work: http://mikepayne.co/2011/remove-category-base-from-url/
These methods also failed: http://www.askapache.com/wordpress/remove-category-wordpress-urls.html
If you want to remove /category/ from the url, follow these two steps:
Go to Settings >> Permalinks and select Custom and enter: /%category%/%postname%/
Next set your Category Base to .
Save it and you’ll see your URL changed to this format:
http://yourblog.com/quotes/
(Source: http://premium.wpmudev.org/blog/daily-tip-quick-trick-to-remove-category-from-wordpress-url/)
If you use Yoast SEO plugin just go to:
Search Appearance > Taxonomies > Category URLs.
And select remove from Strip the category base (usually /category/) from the category URL.
Regarding the tag removal I did not found any solution yet.
Whilst you dismiss it as a solution, the plugin is by far the easiest and most consistent method and they don't change any WordPress default files.
http://wordpress.org/plugins/wp-no-category-base/
It hasn't needed to be updated for a year, so it is not exactly creating any problems with updates.
There is no simple hand rolled solution that will do all of this that does not just replicate what the plugin does from within your own functions.php
Better and logical permalinks like myblog.com/my-category/ and myblog.com/my-category/my-post/.
Simple plugin - barely adds any overhead.
Works out of the box - no setup needed. No need to modify
WordPress files.
Doesn't require other plugins to work.
Compatible with sitemap plugins.
Works with multiple sub-categories.
Works with WordPress Multisite.
Redirects old category permalinks to the new ones (301 redirect, good for SEO).
Plus you get the benefit that if WordPress does change, then the plugin will be updated to work whilst you would then have to figure out how to fix your own code on your own.
Set Custom Structure: /%postname%/
Set Category base: . (dot not /)
Save. 100% work correctly.
instead put this in your functions.php
works fine, no redirect problems.
function fix_slash( $string, $type )
{
global $wp_rewrite;
if ( $wp_rewrite->use_trailing_slashes == false )
{
if ( $type != 'single' && $type != 'category' )
return trailingslashit( $string );
if ( $type == 'single' && ( strpos( $string, '.html/' ) !== false ) )
return trailingslashit( $string );
if ( $type == 'category' && ( strpos( $string, 'category' ) !== false ) )
{
$aa_g = str_replace( "/category/", "/", $string );
return trailingslashit( $aa_g );
}
if ( $type == 'category' )
return trailingslashit( $string );
}
return $string;
}
add_filter( 'user_trailingslashit', 'fix_slash', 55, 2 );
The dot trick will likely ruin your rss feeds and/or pagination. These work, though:
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
$category_rewrite=array();
$categories=get_categories(array('hide_empty'=>false));
foreach($categories as $category) {
$category_nicename = $category->slug;
if ( $category->parent == $category->cat_ID )
$category->parent = 0;
elseif ($category->parent != 0 )
$category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
$category_rewrite['('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$category_rewrite['('.$category_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
$category_rewrite['('.$category_nicename.')/?$'] = 'index.php?category_name=$matches[1]';
}
global $wp_rewrite;
$old_base = $wp_rewrite->get_category_permastruct();
$old_base = str_replace( '%category%', '(.+)', $old_base );
$old_base = trim($old_base, '/');
$category_rewrite[$old_base.'$'] = 'index.php?category_redirect=$matches[1]';
return $category_rewrite;
}
// remove tag base
add_filter('tag_rewrite_rules', 'no_tag_base_rewrite_rules');
function no_tag_base_rewrite_rules($tag_rewrite) {
$tag_rewrite=array();
$tags=get_tags(array('hide_empty'=>false));
foreach($tags as $tag) {
$tag_nicename = $tag->slug;
if ( $tag->parent == $tag->tag_ID )
$tag->parent = 0;
elseif ($tag->parent != 0 )
$tag_nicename = get_tag_parents( $tag->parent, false, '/', true ) . $tag_nicename;
$tag_rewrite['('.$tag_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?tag=$matches[1]&feed=$matches[2]';
$tag_rewrite['('.$tag_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?tag=$matches[1]&paged=$matches[2]';
$tag_rewrite['('.$tag_nicename.')/?$'] = 'index.php?tag=$matches[1]';
}
global $wp_rewrite;
$old_base = $wp_rewrite->get_tag_permastruct();
$old_base = str_replace( '%tag%', '(.+)', $old_base );
$old_base = trim($old_base, '/');
$tag_rewrite[$old_base.'$'] = 'index.php?tag_redirect=$matches[1]';
return $tag_rewrite;
}
// remove author base
add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');
function no_author_base_rewrite_rules($author_rewrite) {
global $wpdb;
$author_rewrite = array();
$authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");
foreach($authors as $author) {
$author_rewrite["({$author->nicename})/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$"] = 'index.php?author_name=$matches[1]&feed=$matches[2]';
$author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
$author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';
}
return $author_rewrite;}
add_filter('author_link', 'no_author_base', 1000, 2);
function no_author_base($link, $author_id) {
$link_base = trailingslashit(get_option('home'));
$link = preg_replace("|^{$link_base}author/|", '', $link);
return $link_base . $link;
}
The non-category plugin did not work for me.
For Multisite WordPress the following works:
Go to network admin sites;
Open site under \;
Go to settings;
Under permalinks structure type /%category%/%postname%/.
This will display your url as www.domainname.com/categoryname/postname;
Now go to your site dashboard (not network dashboard);
Open settings;
Open permalink. Do not save (the permalink will show uneditable field as yourdoamainname/blog/. Ignore it. If you save now the work you did in step 4 will be overwritten. This step of opening permalink page but not saving in needed to update the database.
If you're still searching for the combination (tags, categories and pages on the url-base), you can do it like I did.
Open the settings for permalinks and set a dot (.) for the category- and tag-base (https://premium.wpmudev.org/blog/removing-category-base-urls-wordpress/)
Install the plugin wp-no-tag-base
Tested using Wordpress 3.9.1
If you have pages, categories or tags having the same name, the system will take:
tag
page
category
https://wordpress.org/plugins/remove-category-url/
Use this plugin it does the job perfectly of hiding the category-base
It does not require any setting just install and activate.
Select Custom Structure in permalinks and add /%category%/%postname%/ after your domain. Adding "/" to the category base doesn't work, you have to add a period/dot. I wrote a tutorial for this here: remove category from URL tutorial
I don´t know how to do it using code, but for those who don't mind using a plugin. This is a great one that works for me:
https://es.wordpress.org/plugins/permalink-manager/
Modifying WP core files doesn't seems to be a solution for removing the category prefix. Also the "." fix via the Permalinks doesn't seems to work.
I believe its better to have this set via Yoast SEO plugin or Rank Math SEO Plugin, hoping almost all WordPress sites have either one of this plugin for SEO purpose.
No complicated steps, just a few mouse clicks and eventually forget about it.
If you are using Yoast SEO Plugin,
Yoast SEO > Search Appearance > Taxonomies
If you are using Rankmath SEO Plugin,
Rankmath > General Settings > Strip Category Base
And here is a dedicated plugin that meets the purpose: https://wordpress.org/plugins/remove-category-url/ , if that helps someone.
updated answer:
other solution:
In wp-includes/rewrite.php file, you'll see the code:
$this->category_structure = $this->front . 'category/';
just copy whole function, put in your functions.php and hook it. just change the above line with:
$this->category_structure = $this->front . '/';
add_action( 'init', 'remove_category_perma' );
function remove_category_perma() {
unset($GLOBALS['wp_rewrite']->extra_permastructs['category']);
}
WordPress 5.0.2:
To remove category slug from existing posts, do this :
Navigate to Settings > Permalinks and change Custom Structure from /%category%/%postname%/ to: /%postname%/
Keep Category and Tag bases empty (which is the default also)
Save
All posts can now be directly accessed via domain.com/%postname%/ and all categories can be accessed via domain.com/category/xyz/. WordPress will automatically add all the 301 redirects for the old urls. So, if someone accesses domain.com/%category%/%postname%/, they will automatically get redirected to domain.com/%postname%/.
Adding "." or "/" won't work if you want a consolidated blog view. Also, I have know idea what that solutions would do for the RSS or XML feeds. I feel better sticking with the WP convention. However, I did come up with a more elegant approach.
First, I name the base category url "blog"
Then I created a category called "all". Finally, I but all my subcategories under "all". So I get a url structure like this.
/blog - 404 - recommend 301 redirect to /blog/all/
/blog/all/ - all posts combined.
/blog/all/category1/ - posts filtered by category1
/blog/all/category2/ - posts filterer by category2
I put a custom label on the menu item called "Blog", but it goes to blog/all. It would be a good idea to 301 redirect /blog to /blog/all in the .htaccess file to avoid the 404 on /blog.