Plugin's shortcode not working - wordpress

Hi I have tried as the following
$my_postid = 12;//This is page id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
I have written shortcode in the page with id 12. It doesn't works. Please help!! !

You can use the get_shortcode_regex function to check the post content and isolate the shortcode. Replace my_shortcode with the actual identifier of the shortcode you're trying to get:
$content_post = get_post( 12 );
$content = apply_filters( 'the_content', $content_post->post_content );
$pattern = get_shortcode_regex();
preg_match( '/' . $pattern . '/s', $content, $matches );
if ( is_array( $matches ) && $matches[2] == 'my_shortcode' ) {
$shortcode = $matches[0];
echo do_shortcode( $shortcode );
}

Related

How to add target blank in this woocommerce code

find this code in the business bloomber. I want to add target blank attribute in this code can any help me?
function bbloomer_image_link_external_url( $html,
$post_thumbnail_id ) {
global $product;
if ( ! $product->is_type( 'external' ) ) return $html;
$url = $product->add_to_cart_url();
$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
$html = preg_replace( $pattern, $url, $html);
return $html ;
}
You can use str_replace. just add the below line after preg_replace.
$html = str_replace('<a', '<a target="_blank" ', $html);
Complete code.
function bbloomer_image_link_external_url( $html, $post_thumbnail_id ) {
global $product;
if ( ! $product->is_type( 'external' ) ) return $html;
$url = $product->add_to_cart_url();
$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
$html = preg_replace( $pattern, $url, $html );
$html = str_replace('<a', '<a target="_blank" ', $html);
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'bbloomer_image_link_external_url', 100, 2 );
Tested and works.

Wordpress do_shortcode() with post_content

I want to make the title of any post from post content. So, I made the following in functions.php
function my_title($title)
{
global $post;
// Do whatever here with your title...
$content = $post->post_content;
print $content;
$title =$content. $post->post_title . ' | ' .
get_bloginfo('name');
return $title;
}
It prints shortcode inside post content but if i apply $content = do_shortcode( $content ); it does not produce the actual post content. When i applied $content = do_shortcode( $content ); website hanged. Let me know how to use $content = do_shortcode( $content ); inside this function so that title can be changed.
Can you try this with wp_filter_nohtml_kses. I guess your html o/p is causing the issue
$content = $post->post_content;
$content = wp_filter_nohtml_kses( $content ); // this wp_filter_nohtml_kses indicates strip_tags
$content = do_shortcode( $content );
or
echo do_shortcode(get_post_field('post_content', $post->id));

How to hide oembed links from wordpress excerpt?

the_excerpt() function shows all oembeded links.
In this case is about a youtube video: http://i.stack.imgur.com/MP3EF.png
Adding the following code to the functions.php file (backing up the file first just in case you break the page):
function remove_links($text)
{
if ('' == $text )
{
$pattern = '~http://[^\s]*~i'; //what we want to remove, the http link
$text = get_the_content('');
$text = preg_replace($pattern, '', $text);
}
return $text;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'remove_links');
This will override the default excerpt() function and look for the link and remove it.
Building on #Howli's answer, here's a modified version that also trims the excerpt and applies the proper filters:
function tect_excerpt( $text ) {
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = preg_replace( '~http(s)?://[^\s]*~i', '', $text ); // removes URLs
$text = apply_filters( 'the_content', $text );
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = apply_filters( 'excerpt_length', 20 ); // default: 55
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return $text;
}
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
add_filter( 'get_the_excerpt', 'tect_excerpt');
You may use wp_filter_nohtml_kses function:
echo wp_filter_nohtml_kses(get_the_excerpt());
which will remove the and opening and closing tags

Get images from post's gallery WordPress 3.5

How to get images from a gallery in a post in WordPress 3.5 as gallery is no longer related to posts in 3.5. get_children() doesnot work as gallery is not attachment. Any help is appreciated.
You must probably parse shortcode:
http://codex.wordpress.org/Gallery_Shortcode
Use regular expression:
$post_content = get_the_content();
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);
`global $post;
$post_subtitrare = get_post( $post->ID );
$content = $post_subtitrare->post_content;
$pattern = get_shortcode_regex();
preg_match( "/$pattern/s", $content, $match );
if( isset( $match[2] ) && ( "gallery" == $match[2] ) ) {
$atts = shortcode_parse_atts( $match[3] );
$attachments = isset( $atts['ids'] ) ? explode( ',', $atts['ids'] ) : get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID .'&order=ASC&orderby=menu_order ID' );
}`
The $attachments will get you what you are used to getting prior to WordPress 3.5.

Exclude Terms from custom Taxonomy?

I have a custom post type in which I have custom taxonomies setup.
I want to print out the categories(custom taxonomy) that a post is included in, but exclude one. I cannot find a solution to exclude the category though.
Here is my code to output a list of the categories the custom post type is filed under:
<?php the_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ' ); ?>
How do I exclude a specific category?
Thanks.
You could create a function in your functions.php file that calls get_the_terms to return the list of terms as an array and then remove the item you don't want.
Give this a try:
function get_excluded_terms( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
if(!in_array($term->term_id,$exclude)) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$excluded_terms[] = '' . $term->name . '';
}
}
$excluded_terms = apply_filters( "term_links-$taxonomy", $excluded_terms );
return $before . join( $sep, $excluded_terms ) . $after;
}
and then use it like this:
<?php echo get_excluded_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ', array(667)); ?>

Resources