How can I disable duplicate post check in WordPress importer? - wordpress

I am looking for a way to import a batch of posts with many posts with the same name and publication date.
Code in class-wp-import.php:
$post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post );

disable WordPress default importer duplicate checking:
eg: in functions.php
add_filter( 'wp_import_existing_post', function ( $post_exists ) {
$post_exists = false;
return $post_exists;
} );

Related

wordpress update permalinks structure

I need to change my blog archive pagination url from:
/blog/2
to blog/page/2
This is our current permalink setup under Settings
Custom Structure - /blog/%postname%/
I don't have access to the server, htaccess, or the template files so trying to accomplish this inside functions.php
I am trying to use paginate_links filter to update the 'format' of my url string but does the $link argument have all the details of the paginate_links function, base, format..ect. I tried to use var_dump to see the $link array/object but nothing prints out to the screen.
// define the paginate_links callback
function filter_paginate_links( $link ) {
// Blog page
if ( !is_front_page() && is_home() ) {
var_dump($link);
return $link;
}
return $link;
};
// add the filter
add_filter( 'paginate_links', 'filter_paginate_links', 10, 1 );

Add itemReviewed props into woocommerce product loop

On my woocommerce theme I get some errors on Google structured data such as:
I've found code into mytheme/woocommerce/loop/ratings.php
In this file there are only this method:
$product->get_average_rating();
The question is: there's a hook or action that implement this function?
I need to implement "ItemReviewed" props.
This is down to your product schema, option one is to remove the error but wont push the reviews is
/**
* Remove the generated product schema markup from Product Category and Shop pages.
*/
function wc_remove_product_schema() {
remove_action( 'woocommerce_shop_loop', array( WC()->structured_data, 'generate_product_data' ), 10, 0 );
}
add_action( 'woocommerce_init', 'wc_remove_product_schema' );
if you want to aggregate your reviews you will need to update your schema if you are using yoast and woocommerce this can be achieved by looking into the specific yoast woocoommerce plugin and the plugin docs to add the correct schema
https://developer.yoast.com/schema-documentation/woocommerce-seo/
I had the same problem - reviews were not validating and throwing an error
Managed to get it fixed by editing /wp-content/themes/YOURTHEME/woocommerce/single-product/review.php
I added the below code:
<p>Item Reviewed: <span itemprop="itemReviewed"><?php echo get_the_title(); ?></span></p>
That generated the itemReviewed markup in Structured Data Tool and my reviews validated.
Might want to place it in the child theme just in case :)
define the woocommerce_structured_data_review callback
function filter_woocommerce_structured_data_review( $markup, $comment ) {
global $product;
$markup['itemReviewed']['sku'] = $product->get_sku();
$markup['itemReviewed']['brand'] = $product->get_attribute( 'brand' ) ?? null;
$markup['itemReviewed']['description'] = wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) );
$markup['itemReviewed']['image'] = wp_get_attachment_url( $product->get_image_id() );
$markup['itemReviewed']['isbn'] = $product->get_attribute( 'isbn' ) ?? null;
$markup['itemReviewed']['AggregateRating'] = $product->get_average_rating();
return $markup;
};
Woocommerce Reviews filter
add_filter( 'woocommerce_structured_data_review',
'filter_woocommerce_structured_data_review', 10, 2 );

How to retrieve query list of post_type in WordPress?

I am developing a plugin and need the list of post types existing in the WordPress backend, I checked the WP_Query page of WordPress codex but could not find the solution
You can try this code.
<?php
// hook into init late, so everything is registered
// you can also use get_post_types where ever. Any time after init is usually
fine.
add_action( 'init', 'wpse34410_init', 0, 99 );
function wpse34410_init()
{
$types = get_post_types( [], 'objects' );
foreach ( $types as $type ) {
if ( isset( $type->rewrite->slug ) ) {
// you'll probably want to do something else.
echo $type->rewrite->slug;
}
}
}
i think it will work for you

Remove category base from WordPress url only for specific category

I would like to remove the category base from Wordpress URL only for specific category.
For example, i need to change:
mysite.com/category/blog
to
mysite.com/blog
but i want to keep other categories unchanged:
mysite.com/category/songs
I think that it could be achieved with some .htaccess rule, but I found some generic rules that remove the basic category in all the url.
you can easily achieve this by using Enhanced Custom Permalinks Wp plugin. you just need to go edit the category, yo will see a field to add your custom url.
https://wordpress.org/plugins/enhanced-custom-permalinks/
This can be accomplished with some custom filters & actions.
Try placing this code in your theme's functions.php file:
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the categories for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "News" ) {
$permalink = trailingslashit( home_url('/'. $post->post_name .'-'. $post->ID .'/' ) );
}
return $permalink;
}
add_action('generate_rewrite_rules', 'custom_rewrite_rules');
function custom_rewrite_rules( $wp_rewrite ) {
// This rule will will match the post id in %postname%-%post_id% struture
$new_rules['^([^/]*)-([0-9]+)/?'] = 'index.php?p=$matches[2]';
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite;
}
This will set up the permalink structure that you want for posts:
You can easily do that without using plugins.
Through Admin panel
go to settings->permalinks and select default to custom
here you know more..
https://codex.wordpress.org/Using_Permalinks

How can I get the default content of WordPress post?

Is there a way where I can get the default output of the_content in WordPress post? Some of the plugin is using add_filter to the content to add their desired result like related post plugins where they add the result at the end of the content of a post. What I want to happen is to get the default formatting functions of WordPress core without any additional filters from other plugins.
You can use remove filter function.
remove_filter( $tag, $function_to_remove, $priority );
For more please refer below link
https://codex.wordpress.org/Function_Reference/remove_filter
You can use the_content filter itself to get the post object or the content , you can modify it or you can unhook it and again hook it as per your requirement .
If I have understood your requirement this link will help you, if you need something different please ask me.
Guys thanks for responding to my question I think I got it somehow but need to run more test. What I did was I replicate the process on how the_content() function of WordPress work. Base from my research there are 10 default filters coming from WordPress core you can see on this link: read here
I just created my own function like the_content() from WordPress and apply the default filters to my own function. Put this code in your functions.php
if(!function_exists(default_post_content)){
function default_post_content( $more_link_text = null, $strip_teaser = false) {
$content = get_the_content( $more_link_text, $strip_teaser );
$content = apply_filters( 'default_post_content', $content );
$content = str_replace( ']]>', ']]>', $content );
echo do_shortcode($content);
}
add_filter( 'default_post_content', array( $wp_embed, 'run_shortcode' ), 8 );
add_filter( 'default_post_content', array( $wp_embed, 'autoembed'), 8 );
add_filter ( 'default_post_content', 'wptexturize');
add_filter ( 'default_post_content', 'convert_smilies');
add_filter ( 'default_post_content', 'convert_chars');
add_filter ( 'default_post_content', 'wpautop');
add_filter ( 'default_post_content', 'shortcode_unautop');
add_filter ( 'default_post_content', 'prepend_attachment');
}
Then for example in your single page template(single.php) instead of using the usual the_content I can use my function default_post_content();. Now I don't need to worry about any of the plugins that create additional data to the_content() function.

Resources