Remove WordPress front page rel next - php

Anyone know how to remove default WordPress front page rel next and add custom rel next.
below rel next is wrong.because i am using pagination on my front page.
<link rel='next' title='About Us' href='http://myweb/about-us' />
i want to correct my rel next.any one can help me.
<link rel='next' href='http://myweb/page/2' />
using add_action , and remove_action

Recommended method:
I'm not sure that add_action and remove_action are the right tools for this, but since you are willing to use them, you should be willing to use add_filter - which is the right tool.
There are two filters that you can hook into:
add_filter( "previous_post_rel_link", 'remove_title_from_previous_link' );
add_filter( "next_post_rel_link", 'remove_title_from_next_link' );
Then, write your function to parse / remove the title attribute:
function remove_title_from_previous_link($link) {
// Write your code here to provide and return the correct link
// Sample only below:
return '<a href="my_custom_url" title="Corrected Url" rel="previous">';
}
function remove_title_from_next_link($link) {
// Write your code here to provide and return the correct link
// Sample only below:
return '<a href="my_custom_url" title="Corrected Url" rel="next">';
}
EDIT:
If you really must use add_action and remove_action, then you'd want to hook into this action like so:
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 999);
And then
add_action('wp_head', 'my_custom_rel_link_function');
And of course your custom function:
function my_custom_rel_link_function() {
// ... do your magic here ...
}

This is Only for Front Page
add below code in to the head section
// add custom link rel for home page
if(is_front_page()){
$post_per_page = 1;
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$published_posts = wp_count_posts()->publish;
$prev_link = get_pagenum_link( $paged - 1 );
$next_link = get_pagenum_link( $paged +1 );
if( $paged >= 2 ){
echo "<link rel=\"prev\" href=\"".$prev_link."\"/>"."\n";
}
if( $published_posts > ( $post_per_page * $paged ) ){
echo "<link rel=\"next\" href=\"".$next_link."\"/>"."\n";
}
}

Solution below:
This removes rel="next" & rel="prev" from custom post types and pages.
Add below code in function.php
add_filter( 'wpseo_next_rel_link', '__return_false' );
add_filter( 'wpseo_prev_rel_link', '__return_false' );

Related

WordPress: Overwrite the canonical URL in YoastSEO

I'm using a snippet to overwrite the canonical URL in YoastSEO for WordPress/WooCommerce. The snippet is based on the official docs example: https://developer.yoast.com/features/seo-tags/canonical-urls/api/
Here's my code:
function prefix_filter_canonical_example( $canonical ) {
if (is_shop() && is_paged() ) :
$canonical = get_permalink(woocommerce_get_page_id( 'shop' )).'page/'.get_query_var('paged').'/';
elseif(WCV_Vendors::is_vendor_page()):
$vendor_shop = urldecode( get_query_var( 'vendor_shop' ) );
$vendor_id = WCV_Vendors::get_vendor_id( $vendor_shop );
$canonical = WCV_Vendors::get_vendor_shop_page( $vendor_id );
endif;
return $canonical;
}
add_filter( 'wpseo_canonical', 'prefix_filter_canonical_example' );
The code doesn't do anything to the canonical URL regardless of the content I return. But the if/else works fine and if I echo the content of $canonical I see the correct URLs.
I tried it already with the basic storefront theme and I deactivated nearly all plugins. But the snippet won't work. Is there anything I miss?
If you don't pass any priority to the action wordpress will take it as 10. Yoast SEO also call this filter to add canonical url so wordpress executed function in the order in which they were added to the action.
So change this line
add_filter( 'wpseo_canonical', 'prefix_filter_canonical_example' );
With this
add_filter( 'wpseo_canonical', 'prefix_filter_canonical_example', 20 );

Customise Woocommerce cart widget

I am trying to customise the woocommerce cart widget and make it more "visual" like the below example. I have researched and it seems I may be able to override the woocommerce cart widget with my own behaviour through the hook woocommerce_mini_cart().
End result I would like to get to:
Is it possible to modify the core functionality of the cart widget via this approach to achieve something like this, through the functions.php file or would I need CSS aswell?
if ( ! function_exists( ‘woocommerce_mini_cart’ ) ) {
function woocommerce_mini_cart( $args = array() ) {
$defaults = array( ‘list_class’ => ” );
$args = wp_parse_args( $args, $defaults );
woocommerce_get_template( ‘cart/mini-cart.php’, $args );
}
//*MODIFY HERE?*
}
Alternatively does anyone know of a woocommerce plugin which could solve this?
Despite of the question is not actual for its` author already,
I wanna to share some code to modify the default woocommerce_mini_cart() output:
1) The most difficult step - disable ECHO for woocommerce_mini_cart() function
2) change output HTML as you wish;
3) echo formatted HTML.
1)
function disable_echo_for_woocommerce_mini_cart() {
$mini_cart_html = woocommerce_mini_cart();
return $mini_cart_html;
}
2)
$mini_cart_html = disable_echo_for_woocommerce_mini_cart();
$mini_cart_html = str_replace('some_code', 'some_code_2', $mini_cart_html); // change output HTML.
$mini_cart_html = ob_get_clean();
3)
echo $mini_cart_html;

Adding an image page banner to shop page

I am using WordPress 4.9.6.
I have set the shop page to be the home-page.
How do I add a page banner to the shop page. I would like to add it just above the breadcrumb trail.
I have tried adding this to the following page archive-product.php
if (is_shop()) {
$args = array('taxonomy' => 'product_cat');
$product_categories = get_categories( $args );
$term_id = $product_categories[0]->term_id;
$content = get_term_meta($term_id, 'cat_meta');
if(isset($content[0]['cat_header'])){
echo do_shortcode($content[0]['cat_header']);
}
}
Unfortunately, not able to add any image to the page.
You can achieve using 2 methods.
1) Add your static image directly at the beginning of archive-product.php
echo "<img src='{YOUR_IMAGE_PATH}'>";
2) Add filter in your theme's functions.php file.
add_action ('woocommerce_archive_description' , 'shop_banner',99);
function shop_banner() {
echo '<img src="{YOUR_IMAGE_PATH}" >';
}
I'm not so sure if I understand exactly what you want. But this is what I understand so far.
If you want to display an Static image banner above the breadcrumbs in your Shop Page.
You could use the woocommerce_before_main_content action.
function BannerShop(){
if(is_shop()){
echo '<img src="https://localhost/demosite/wp-content/uploads/2015/06/512x356.png" >';
}
}
add_action( 'woocommerce_before_main_content', 'BannerShop', 10 );
Here i show the before and after. BTW I don't know what theme are you using so it may be displayed different.
Before
https://i.stack.imgur.com/Mv2YK.jpg
After https://i.stack.imgur.com/nTfCa.jpg

Can't remove canonical link in WordPress with Yoast SEO Plugin

So I'm trying to remove the canonical link in the header of WordPress for paginated pages but all the suggestions I tried aren't working.
Here is my code which is in my functions.php file:
function my_add_noindex_tags(){
$paged = intval( get_query_var( 'paged' ) );
if(is_search() || is_404() ) :
echo '<meta name="robots" content="noindex,follow">';
endif;
if ($paged >= 2) :
add_filter( 'wpseo_canonical', '__return_false', 10, 1 );
remove_action('wp_head', 'rel_canonical');
echo '<meta name="robots" content="noindex,follow">';
endif;
}
add_action('wp_head','my_add_noindex_tags', 4 );
I know the code inside if ($paged >= 2) : runs because this tag <meta name="robots" content="noindex,follow"> is in the head section.
Anyone know where I might be going wrong.
The issue here is the canonical link added by Yoast SEO aren't properly removed as expected.
Cheers
After going through the Yoast SEO codes, it seems like the canonical action is added to wpseo_head action.
You either have to use priority 1 when adding your function to run in wp_head to get this to execute properly, or do it with the appropriate method below ie. using wpseo_head action.
function remove_canonical() {
$paged = intval( get_query_var( 'paged' ) );
if ($paged >= 2) {
add_filter( 'wpseo_canonical', '__return_false', 10 );
}
}
add_action( 'wpseo_head', 'remove_canonical', 4);
For me only adding this to init action worked
add_action('init', function() {
add_filter( 'wpseo_canonical', '__return_false', 10 );
});
All these answers didn't help me and i couldn't find the right answer for my scenario:
I had to set priority to higher than 10 because apparently Yoast changes the url at priority 10 so if you want to change their url you have to set a higher priority:
Change the canonical url with a custom function:
add_filter( 'wpseo_canonical', 'change_canonical', 20 );
Delete the canonical url:
add_filter( 'wpseo_canonical', '__return_false', 20 );
Be aware that if you do not set a priority it will use priority 10 as the default and it will not work.
I know I'm late to the party, but for the right implementation, you need to run 'wpseo_canonical' filter directly:
function remove_canonical_pagination() {
$paged = intval( get_query_var( 'paged' ) );
if ($paged >= 2) {
return false;
}
}
add_action( 'wpseo_canonical', 'remove_canonical_pagination', 4);

Use wordpress shortcode in post title

I am trying to use the following shortcode in the wordpress post title. The shortcode looks like the following:
//Use [year] in your posts.
function year_shortcode() {
$year = date('Y');
return $year;
}
add_shortcode('year', 'year_shortcode');
Any suggestions how to execute this shortcode in the post title?
I appreciate your replies!
You can absolutely use a shortcode in a title. You just need to use the WordPress hooks system to run the shortcode when the title is called. So if you want to have a shortcode [year] that spits out the current year, you'll create the shortcode:
add_shortcode( 'year', 'sc_year' );
function sc_year(){
return date( 'Y' );
}
Then, hook into the filter for the_title() to run your shortcode:
add_filter( 'the_title', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
That takes care of the Post/Page title, but you'll also want to run it for the single_post_title hook which is used in wp_head on the title tag on your site. That way, the browser will show the proper title as well:
add_filter( 'single_post_title', 'my_shortcode_title' );
Note: You don't need a separate function here because it's running the exact same code. So your total code would look something like this:
add_shortcode( 'year', 'sc_year' );
function sc_year(){
return date( 'Y' );
}
add_filter( 'single_post_title', 'my_shortcode_title' );
add_filter( 'the_title', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
I don't think you can apply (save in admin post edit screen) a shortcode in the post title with a shortcode. The post_title is sanitize to avoid tag or shortcode, the post title is used by many function that a shortcode can break.
To make modification on the post_title, you can use the filter the_title
add_filter('the _title', 'yourfunction');
function yourfunction($title){
global $post;
// if you set a custom field on the post where you want to display the year
if(get_post_meta($post->ID, 'display_year', true) == 1){
$title = $title. ' '. date('Y');
}
return $title;
}
Hope it helps
Please add this code in 'function.php'. Try this.
<?php
function TitleFunction($title)
{
global $post;
$title = $title. ' ' .get_the_date('Y');
return $title;
}
add_filter( 'the_title', 'TitleFunction', 10, 2 );
?>

Categories