How to set AMP meta description with functions.hp in WP - php

I set the php code for meta description in normal entrys in WP, and I have set an additional php code for AMP template to replicate the same text in the meta description tag but I doný know how to vinculate them.
this is php code for generic meta description:
function wpse_custom_meta_description(){
if ( ! is_single() && ! is_page())
return;
$desc = get_post_meta( get_queried_object_id(), 'description', true );
if( ! empty( $desc ) )
printf(
'<meta name="description" content="%s" />',
esc_attr( trim( $desc ) )
);
}
add_action( 'wp_head', 'wpse_custom_meta_description' , 2 );
...and this is the code for meta description in AMP, but I would like that both formats had the same text, how can I vinculate AMP text to generic?
add_action('amp_post_template_head', function() {
echo '<meta name="description" content="'wpse_custom_meta_description'">';
});
I mean, in the AMP php code I need to vinculate to "wpse_custom_meta_description" function.
Thank you!

Related

nav_menu_item_title stripping most HTML

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.

Add Html code with get post meta

I am confused with a simple issue, Actually, I am using Wordpress Custom fields using post meta
echo get_post_meta( $post->ID, '_text_field', true );
Above meta code generate a value entered in a custom field.
But, I want to add html code around it.
If i add paragraph to it like this <p> <?php echo get_post_meta( $post->ID, '_text_field', true ); ?> </p> then it is fine! but when field is empty then it shows a empty <p></p>
That's why i am looking to add a html in meta code so that if field is empty then no html code will be included in DOM.
And if field have text then it shows text with a html markup.
i am not sure how to do this!
Any idea?
Use the wpautop() function to automatically add paragraphs instead.
Example:
echo wpautop( get_post_meta( $post->ID, '_text_field', true ) );
You should probably be assigning this meta field to a variable first and checking the value is set before you attempt to output.
E.g.
$text_field = get_post_meta( $post->ID, '_text_field', true );
if ( ! empty( $text_field ) ) {
echo wpautop( $text_field );
}

Extracting a WooCommerce Attribute and Put it in Google Book Viewer Script

I'm going to use Google books embedded viewer in my website which runs using Wordpress CMS and Woocommerce plugin.
My idea is that one Woocommerce attribute for each product (here a book) be assigned to relevant book Google books ID (something like 0738531367), then using a method, this string be inserted to viewer script which simplest form of it follows,
<script type="text/javascript" src="http://books.google.com/books/previewlib.js"></script>
<script type="text/javascript">
GBS_insertEmbeddedViewer('4rghAwAAQBAJ',600,500);
</script>
Now, how can I extract this string (attribute) from Woocommerce and put it in the viewer script?
More Specific Explanation
This is a book page (a WooCommerce product page).
The book this page offers, has been given an attribute with the value of Google Books ID. It can be found at the lowest row of "مشخصات کتاب" tab.
After inserting this ID into the above code and putting the whole modified code in book page HTML code, the famous Google Books Embedded Viewer corresponding to this book, appeared in the "توضیح ناشر" tab.
But this process is time-consuming and practically impossible when I'm faced with huge number of books to be added.
It is quite simple to embed the Viewer code in the WooCommerce (or theme) TEMPLATE page ONCE. So, for each books the only effort is to defining the so-called book ID in an predefined attribute.
After all, one task remains: transferring this ID to the Viewer code. I think it can be handled using a Wordpress hook, php function or the like. Unfortunately searching the net yielded no results (including Google Books Support Forum and Wordpress support directory).
It's possible to integrate into WC product meta box (also, see docs), but I'll show how to do it with a normal meta box.
<?php
/**
* Plugin Name: (SO) Book ID
*/
add_action( 'add_meta_boxes', 'add_box_so_26564103' );
add_action( 'save_post', 'save_so_26564103', 10, 2 );
function add_box_so_26564103() {
add_meta_box(
'sectionid',
'Book ID',
'inner_box_so_26564103',
'product',
'side',
'low'
);
}
function inner_box_so_26564103($post)
{
wp_nonce_field( plugin_basename( __FILE__ ), 'noncename' );
$saved = get_post_meta( $post->ID, '_book_id', true);
printf(
'<label>Enter the ID <input type="text" name="_book_id" value="%s" /></label>',
$saved ? $saved : ''
);
}
function save_so_26564103( $post_id, $post_object )
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !wp_verify_nonce( $_POST['noncename'], plugin_basename( __FILE__ ) ) )
return;
if ( 'revision' == $post_object->post_type )
return;
if ( isset( $_POST['_book_id'] ) )
update_post_meta( $post_id, '_book_id', $_POST['_book_id'] );
else
delete_post_meta( $post_id, '_book_id' );
}
Then, in your template, just retrieve the meta data:
<script type="text/javascript" src="http://books.google.com/books/previewlib.js"></script>
<script type="text/javascript">
GBS_insertEmbeddedViewer('<?php echo get_post_meta( $post->ID, "_book_id", true); ?>',600,500);
</script>

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