I'm trying to remove date properties from Article schema generated by the Yoast SEO plugin.
In their developer docs the wpseo_schema_article filter is set as an example for manipulating with Article graph piece. However even with this type="application/ld+json":
<script type="application/ld+json">
{
"#context":"https://schema.org",
"#type":"Article",
"mainEntityOfPage":{
"#type":"WebPage",
"#id":"https://www.myproscooter.com/etwow-electric-scooters-review/"
},
"headline":"E-Twow Electric Scooters 2021 Review",
"image":{
"#type":"ImageObject",
"url":"https://www.myproscooter.com/wp-content/uploads/2020/12/elek-scoot.jpg",
"width":700,
"height":400
},
"datePublished":"2020-12-08T08:52:13",
"dateModified":"2021-01-12T11:30:10",
"author":{
"#type":"Person",
"name":"Jason"
},
"publisher":{
"#type":"Organization",
"name":"MyProScooter",
"logo":{
"#type":"ImageObject",
"url":"https://www.myproscooter.com/wp-content/uploads/2021/01/MPS-Logo-228x60.png"
}
}
}
</script>
When I try to access and manipulate data like this:
add_filter( 'wpseo_schema_article', 'remove_article_dates' );
function remove_article_dates( $data ) {
file_put_contents(WP_CONTENT_DIR.'/helper-seo.txt','DATA PRE FILTER: '.print_r($data,true),FILE_APPEND);
unset($data['datePublished']);
unset($data['dateModified']);
return $data;
}
Nothing gets logged into helper-seo.txt nor do dates get unset in the Article schema; as if the filter is ignored totally.
What's more confusing is that manipulation with dates in Webpage Schema works and is similar to the above:
add_filter( 'wpseo_schema_webpage', 'remove_webpage_dates');
function remove_webpage_dates( $data ) {
unset($data['datePublished']);
unset($data['dateModified']);
return $data;
}
The other stuff I've tried include:
add_filter( 'wpseo_schema_article_date_published', '__return_false' );
add_filter( 'wpseo_schema_article_date_modified', '__return_false' );
Which isn't reflecting into Article schema at all. How to remove these properties sucessfully?
i am using this code, it will work
add_filter ( 'wpseo_schema_webpage' , 'remove_breadcrumbs_property_from_webpage' , 11 , 1 ) ;
function remove_breadcrumbs_property_from_webpage( $data ) {
if (array_key_exists('datePublished', $data)) {
unset($data['datePublished']);
unset($data['dateModified']);
}
return $data;
}
Try this exact code. I'm sure it will work for you. This cleared my problems at least.
// Remove DatePublished
add_filter( 'wpseo_schema_graph_pieces', 'remove_datePublished_from_schema', 11, 2 );
add_filter( 'wpseo_schema_webpage', 'remove_datePublished_property_from_webpage', 11, 1 );
/**
* Removes the DatePublished graph pieces from the schema collector.
*
* #param array $pieces The current graph pieces.
* #param string $context The current context.
*
* #return array The remaining graph pieces.
*/
function remove_datePublished_from_schema( $pieces, $context ) {
return \array_filter( $pieces, function( $piece ) {
return ! $piece instanceof \Yoast\WP\SEO\Generators\Schema\datePublished;
} );
}
/**
* Removes the DatePublished property from the WebPage piece.
*
* #param array $data The WebPage's properties.
*
* #return array The modified WebPage properties.
*/
function remove_datePublished_property_from_webpage( $data ) {
if (array_key_exists('datePublished', $data)) {
unset($data['datePublished']);
}
return $data;
}
Related
Hi so I would like to be able to change currency for different products added to the cart/checkout.
I found solution that is filtering woocommerce_currency hook and using cart session to change currency.
Two questions:
Is there a better way of doing it?
Do you see any potential problems with the following solution?
public function modifyWoocommerceCurrency( $currency ) {
$cart_data = WC()->session->get('cart');
foreach ($cart_data as $item) {
if (isset($item["_campaign_options"]["user_paying_currency"])) {
$currency = $item['_campaign_options']['user_paying_currency'];
}
}
return $currency;
}
add_filter( 'woocommerce_currency', [$this,'modifyWoocommerceCurrency'], 10, 1 );
On my Wordpress homepage, the post is showing only one line of description. How do I add new lines?
I want to show more lines. So how can I do it?
Add below shared code in your theme function.php file:-
/**
* Filter the except length to 100 words.
*
* #param int $length Excerpt length.
* #return int (Maybe) modified excerpt length.
*/
add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );
function wpdocs_custom_excerpt_length( $length ) {
return 100;
}
I already created a function that will filter products using URL parameters and display them on the /shop/ page. That works fine.
But, that only works if the products template is set on the WooCommerce /shop/ page.
In my case the requirement is to have the category list set as default template on the /shop/ page. That means I need to override that template and show the filtered product list, when I run a product query with URL parameters.
(eg. example.com/shop/?param1=foo¶m2=bar)
I tried following code (and some iterations), but I'm stuck and have no clue how to override the default template when a product filter is applied:
add_filter( 'woocommerce_locate_template', [$this, 'my_include_template_function'], 10, 3 );
public function my_change_template_function($template, $template_name, $template_path)
{
if (!empty($_GET['param1']) && is_shop() && $template_name == 'content-product_cat.php') {
return wc_get_template_part('content', 'product');
} else {
return $template;
}
}
So the source of the categories loop is added in to the loop start [via a filter][1]
add_filter( 'woocommerce_product_loop_start', 'woocommerce_maybe_show_product_subcategories' );
and the woocommerce_maybe_short_product_subcategories() function changes the output based on the:
$display_type = woocommerce_get_loop_display_mode();
However, the woocommerce_get_loop_display_mode() result isn't filterable directly.
But it relies on some values from get_option() (since the setting is in the customizer) and get_option() is filterable via option_$option
So a potential way to tell WooCommerce not to display the categories when you have a particular $_GET parameter, you could filter the shop display mode option to be products when that parameter is detected in the URL. Like this:
/**
* Filter shop display type.
*
* #param string $value - 'products' | 'subcategories' | 'both'
* #return string
*/
function kia_woocommerce_shop_page_display( $value ) {
if ( ! empty( $_GET['param1'] ) {
$value = 'products';
}
return $value;
}
add_filter( 'option_woocommerce_shop_page_display', 'kia_woocommerce_shop_page_display' );
This question already has answers here:
WooCommerce: Set country by default in checkout page
(5 answers)
Closed 2 years ago.
in the checkout of my Woocommerce-Shop is a dropdown to choose your country.
On default the USA is automatically selected already.
How to just have a placeholder with “select your country” instead?
I couldn’t find any solution to this topic somebody have any idea?
I changed the other Placeholders which aren't Dropdowns but are inside the same form:
add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 );
function override_billing_checkout_fields( $fields ) {
$fields['billing']['billing_first_name']['placeholder'] = 'First Name*';
$fields['billing']['billing_last_name']['placeholder'] = 'Last Name*';
$fields['billing']['billing_city']['placeholder'] = 'Town / City*';
$fields['billing']['billing_postcode']['placeholder'] = 'ZIP*';
$fields['billing']['billing_email']['placeholder'] = 'Email Address*';
return $fields;
}
My poor solution for now is: I created a new "Country in the Dropdown List" which I just named: select your country* and then just selected it on default instead of the USA. But the problem is here that the system thinks a real country is chosen already so it's not a mandatory field anymore and also the fact that it just doesn't look usual for the user when they choose their country:
function woo_add_my_country( $country ) {
$country["PLACE"] = 'select your country*';
return $country;
}
add_filter( 'woocommerce_countries', 'woo_add_my_country', 10, 1 );
add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );
function bbloomer_change_default_checkout_country() {
return 'PLACE';
}
I would appreciate any help or tips!
This is working for my side try this
// Change the default country and state on checkout page.
// This works for a new session.
add_filter( 'default_checkout_country', 'xa_set_default_checkout_country' );
add_filter( 'default_checkout_state', 'xa_set_default_checkout_state' );
function xa_set_default_checkout_country() {
// Returns empty country by default.
return null;
// Returns India as default country.
// return 'IN';
}
function xa_set_default_checkout_state() {
// Returns empty state by default.
return null;
// Returns Madhya Pradesh as default state.
// return 'MP';
}
function woo_add_my_country( $country ) {
$country["PLACE"] = 'select your country*';
return $country;
}
add_filter( 'woocommerce_countries', 'woo_add_my_country', 10, 1 );
add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );
function bbloomer_change_default_checkout_country() {
return 'PLACE';
}
Just go to WooCommerce > Settings.
On General tab you will find Default customer location. Set it to No location by default
This was one of the first results that popped up for me when searching how to set a placeholder for a select field on the Woocommerce checkout page, so for any future visitors:
I have a company select field that I populate dynamically with values from the database. In order to set a placeholder value, I just add an option with a blank value.
add_filter( 'woocommerce_checkout_fields' , 'populate_company_field' );
function populate_company_field($fields) {
$results = get_results_from_database();
$options[''] = __('Choose your company', 'gs'); //add blank value = placeholder
foreach ($results as $result) {
if (!empty($result->company)) {
$options[$result->ID] = $result->company;
}
}
$fields['billing']['company']['options'] = $options;
return $fields;
}
This filter work well.
<pre><code>
function wc_diff_taxes_for_ajax( $tax_class, $product ) {
$tax_class = 'Zero Product';
return $tax_class;
}
add_filter( 'woocommerce_product_tax_class', 'wc_diff_taxes_for_ajax', 1, 2 );
</code></pre>
I try to fire filter inside my other function but always have an error...
The Ajax part call well and get response. So i don't put it here.
<pre><code>
function manage_taxes_aliquota() {
if(isset($_POST['tax_class'])) {
<--What i PUT HERE TO FIRE THE FILTER AND PASS NEW $tax_class-->
}
echo 'ok';
exit;
}
add_action('wp_ajax_nopriv_manage-taxes-aliquota', 'manage_taxes_aliquota');
add_action('wp_ajax_manage-taxes-aliquota', 'manage_taxes_aliquota');
</code></pre>
Regards.