nav_menu_item_title stripping most HTML - php

I am trying to insert some HTML into the title of menu items, however from my simple tests it does not appear to be working. I am running this code in my functions.php file
function show_menu_item_desc( $title, $item ) {
if( is_object( $item ) && isset( $item->ID ) ) {
$title="<span class='blabla'>TEST <br> TEST</span>";
}
return $title;
}
add_filter( 'nav_menu_item_title', 'show_menu_item_desc', 10, 2 );
However it then displays as
<span>TEST TEST</span>
However from reading the documentation on the wordpress website for the nav_menu_item_title filter, the example includes HTML classes. So i am not sure why its being stripped.

Related

Add custom css class for the search page filter

I'm trying to add a custom CSS class for the search page that uses WooCommerce Product Search.
Search pages path is like:
domain.com/shop/ixwpss=3&title=0&excerpt=0&content=0&categories=0&attributes=0&tags=1&sku=0&v=a3420e5a4c03
When I search for the number 3 that represents the tag for products.
I managed to add the class for the whole shop, with the following function but I want to add only for the search page. Can someone help me?
add_filter( 'body_class', 'add_body_classes' );
function add_body_classes( $classes ) {
global $post;
if ( is_shop() )
$classes[] = 'search';
return $classes;
}
Please update the code like below
add_filter( 'body_class', function( $classes ) {
if ( is_shop() ) {return array_merge( $classes, array( 'class-name' ) );}
}
Assuming that the search page is using the archive-product.php
If you are using a WooCommerce compatible theme then you have to go in your themes folder -> woocommerce and find the archive-product.php. If the override file does not exists then go to wp-content/plugins/woocommerce/templates/archive-product.php
And a if statement to check if is search or not like below:
if ( is_search() ) {
<body class="your-class">
} else {
<body class="regular-class">
// the content that is already in that file (archive-product.php)
}
*if the part (body class in your case) is not there you have to locate it and do the same (probably it's in your header.php)

Show custom AND standard products atrributes on Product Single page WooCommerce

I have searched all over for this, and I have found a great code to get the standard product attributes displayed, but the custom ones (that do not start with pa_) are not being displayed.
First things first, the setup:
I have created a product single template with Elementor and normally I can just add the module additional information and I am done. I've done it before. :)
But now I have created a lay-out where these product attributes are being displayed in an accordion in which you cannot place this module.
So after a loooooong search online I have found a snippet made by helgatheviking to create a shortcode to call all the product attributes:
/**
* Attributes shortcode callback on productpage.
*/
function so_39394127_attributes_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attributes' => array_keys( $product->get_attributes() ), // by default show all attributes
), $atts );
// is pass an attributes param, turn into array
if( is_string( $args['attributes'] ) ){
$args['attributes'] = array_map( 'trim', explode( '|' , $args['attributes'] ) );
}
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( ! empty( $args['attributes'] ) ){
foreach ( $args['attributes'] as $attribute ) {
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $attribute, 'pa_' ) === false ? wc_attribute_taxonomy_name( $attribute ) : $attribute;
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
// Build the html string with the label followed by a clickable list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= get_the_term_list( $product->get_id(), $taxonomy, '<li class="prod-attr">' . $attribute_label . ': ' , ', ', '</li>' );
}
}
// if we have anything to display, wrap it in a <ul> for proper markup
// OR: delete these lines if you only wish to return the <li> elements
if( $html ){
$html = '<ul class="product-attributes">' . $html . '</ul>';
}
}
return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );
After adding the shortcode [display_attributes] in the accordion, I get all the standard WC attributes shown that have been created via the standard way: products -> attributes -> create new. So that is great!
But not the custom attributes, that you create inside the productpage itself -> Product data -> custom product attribute -> add (screenshot of what I mean: https://gyazo.com/f2619c7f11d295f4897b38f08d76453b)
Screenshot product page custom attributes are not being shown on front: https://gyazo.com/4c126f7082c6a856d1cf30b712daa37f
I think this is because that these custom product attributes do not contain the pa_ part in front of the meta value like for instance pa_type-speaker (self-created standard attribute).
How can I get the custom attributes also shown? I have tried to remove the pa_ from the code above, but that does not work and I cannot find what to do next. Can you guys help me out here? thx!
Elementor has helped me out on this one!
The solution:
Open the product single template page, Add the additional information module in a row. Create a global widget of this object. Add the shortcode of this global widget in the tabbed module and your done. :)

Moving Page/Post Titles outside the Loop in WordPress

I have a client site running on WordPress. It uses WooThemes Canvas as its base theme (with a customized child theme), and utilizes the Time.ly All-in-one-Event Calendar. It can be viewed here.
The design I came up with required me to move the page/post titles outside the main content area. I followed the instructions on this WooThemes customization document, and made modifications based on the comments so that it would also work for posts and the Time.ly event postings. The entries in my functions.php file looked like this:
add_filter( 'the_title', 'woo_custom_hide_single_post_title', 10 );
function woo_custom_hide_single_post_title ( $title ) {
if ( is_singular( array( 'post', 'ai1ec_event' ) ) && in_the_loop() ) { $title = ''; }
return $title;
} // End woo_custom_hide_single_post_title()
add_filter( 'the_title', 'woo_custom_hide_single_page_title', 10 );
function woo_custom_hide_single_page_title ( $title ) {
if ( is_page() && in_the_loop() ) { $title = ''; }
return $title;
} // End woo_custom_hide_single_page_title()
// Add Titles above Content Area on all Posts & Pages
add_action( 'woo_content_before_singular-post', 'woo_custom_add_title', 10 );
add_action( 'woo_content_before_singular-ai1ec_event', 'woo_custom_add_title', 10 );
add_action( 'woo_content_before_singular-page', 'woo_custom_add_title', 10 );
function woo_custom_add_title () {
if ( ! is_page_template(array('template-biz.php', 'template-brewpub.php')) ) {
global $post;
$title = '<h1 class="page-title">' . get_the_title( $post->ID ) . '</h1>' . "";
echo '<div id="title_container"><header class="col-full">';
echo $title;
woo_post_meta();
echo '</header></div>';
}
} // End woo_custom_add_post_title()
This produced some undesirable results, which can be seen on the DEV site I setup for this post (dev.thebrewworks.com):
Since the Time.ly event calendar also uses the_title to display it's events, in the three instances I used the calendar inside a page loop (the homepage and the two restaurant location pages), the event titles don't show up. This was confirmed by creating a Test Page (dev.thebrewworks.com/test-page), where you can see two instances of the calendar inside the loop, and one outside (in the sidebar). The two instances in the Loop have no titles, while the sidebar does.
Supressing the_title in the Loop didn't suppress the Post Meta, which still shows up in the main content area, but I want it under the title in the new DIV, so reposting woo_post_meta outside the Loop leaves me with two instances of the Post Meta.
In order to get the site live I had to make some concessions in my functions.php file:
// Hide Titles on All Posts & Pages
add_filter( 'the_title', 'woo_custom_hide_single_post_title', 10 );
function woo_custom_hide_single_post_title ( $title ) {
if ( is_singular( array( 'post', 'ai1ec_event' ) ) && in_the_loop() ) { $title = ''; }
return $title;
} // End woo_custom_hide_single_post_title()
add_filter( 'the_title', 'woo_custom_hide_single_page_title', 10 );
function woo_custom_hide_single_page_title ( $title ) {
if ( is_page() && in_the_loop() && is_page_template( array('page.php', 'template-contact.php' ) ) ) { $title = ''; }
return $title;
} // End woo_custom_hide_single_page_title()
// Add Titles above Content Area on all Posts & Pages
add_action( 'woo_content_before_singular-post', 'woo_custom_add_title', 10 );
add_action( 'woo_content_before_singular-ai1ec_event', 'woo_custom_add_title', 10 );
add_action( 'woo_content_before_singular-page', 'woo_custom_add_title', 10 );
function woo_custom_add_title () {
if ( ! is_page_template(array('template-biz.php', 'template-brewpub.php')) ) {
global $post;
$title = '<h1 class="page-title">' . get_the_title( $post->ID ) . '</h1>' . "";
echo '<div id="title_container"><header class="col-full">';
echo $title;
woo_post_meta();
echo '</header></div>';
}
} // End woo_custom_add_post_title()
// Disable Post Meta
function woo_post_meta() {}
?>
Note the changes:
I disabled the post_meta completely. Not exactly what I was looking for, but better than having it show up twice.
I modified the function that disables the page title so that it only works on pages that use certain templates (the three instances I mentioned above use the Business Template in Canvas that already suppresses the page title). This would require me to go in and add each new page template that I want to suppress the title on, though...and on top of that, it's not working. Take a look at the live site. The page title is showing up twice on static pages. Once inside the white content area and once outside in the yellow header.
I'm no PHP-developer...I'm more of an Design/HTML/CSS person. I know enough to be dangerous...how to modify existing code, etc., but not necessarily how to write my own from scratch. I have no idea if the way I came up with things is even the best/most elegant way to go about it. Now that you have all the facts, here are my questions:
In an ideal world, the Post Meta would ONLY show up on the blog entries (not on the calendar entries...no need for them there, really), and OUTSIDE the main content area underneath the page title that I moved. How can I achieve this? I'm guessing it's going to be some sort of conditional statement that removes the woo_post_meta from all instances inside the loop, and adds it to the woo_custom_add_title ONLY if it's a standard post, but how to get there I have no idea.
How can I suppress the_title that shows up at the top of the content area on ALL static pages, regardless of template, but have it not affect instances of the_title that appear elsewhere in the loop (like the calendar syndication). In my head there has to be a way to indicate that you ONLY want to disable the_title if it's in a certain div/id, etc., but again...only enough to be dangerous, unless I see it done elsewhere, I can't come up with the solution on my own.
Thanks in advance for your input.

woo commerece short codes not working on all posts

I have created a short code to display short description in woo commerce but it is not working on all posts. It is displaying the short description on some posts and not on others.
Function to create that short code in functions.php
function product_shortdesc_shortcode( $atts ){
// use shortcode_atts() to set defaults then extract() to variables
extract( shortcode_atts( array( 'id' => false ), $atts ) );
// if an $id was passed, and we could get a Post for it, and it's a product....
if ( ! empty( $id ) && null != ( $product = get_post( $id ) ) && $product->post_type = 'product' ){
// apply woocommerce filter to the excerpt
echo apply_filters( 'woocommerce_short_description', $product->post_excerpt );
}
}
// process [product_shortdesc] using product_shortdesc_shortcode()
add_shortcode( 'product_shortdesc', 'product_shortdesc_shortcode' );
The way i am getting the data in my single.php file
$custom = get_post_custom(get_the_ID());
$my_custom_field = $custom['woo_id'];
foreach ( $my_custom_field as $key => $value ) {
echo do_shortcode('[product_shortdesc id='.$value.']');
}
PS: in my normal post i have a custom field which has the value of product id of the product in woo commerece.
Your issue is that you are expecting shortcodes to function which no longer exist. On new installs, these pages won't be created, but if you are updating you may already have those pages in place.
Although the upgrade script does attempt to trash them for you, this might not have happened if they were customised. Delete them. Delete edit-account and change password, then go to your 'my account' page and click the change password/edit account links. You'll be taken to and endpoint which offers the same functionality.
Thanks
Short Code must not echo code instead return the things that needs to be rendered
Change this
echo apply_filters( 'woocommerce_short_description', $product->post_excerpt );
to
return apply_filters( 'woocommerce_short_description', $product->post_excerpt );

Function Reference/shortcode exists in wordpress

how can i understand a short code is exist in my wordpress post?
[tbps id="1"] is the short code . Where id may vary . ie 2,3 ...
i paste this code in wordpdpress post namely mypost ,it is working .So how can i get the shortcode which exists in wordpress page (in mypost). i don't know how to use this code
<?php
if ( shortcode_exists( 'yourCondition ' ) ) {
//Here i don't know how to use 'yourCondition' .Because condition
//is varying .It may be [tbps id="1"] ,[tbps id="2"] ,[tbps id="3"] etc
}
?>
<?php
if ( shortcode_exists( 'yourCondition ' ) ) {
// The [gallery] short code exists.
}
?>
you can try
In wordpress 3.6 and above you can do this... Say your shortcode like this... [fts youtube username=GoProCamera vid_count=5]
// Detect if the shortcode exists in a post page or widget
global $post;
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'fts youtube') ) {
wp_enqueue_style( 'fts_youtube_css', plugins_url( 'youtube/css/styles.css', dirname(__FILE__) ) );
}
This works even if you use multiple shortcodes on a page, post or widget, and of course will not duplicate your styles or js because you are enqueueing them. Tested and confirmed. https://codex.wordpress.org/Function_Reference/has_shortcode

Categories