WordPress - Overwriting a Shortcode - php

I have a theme that extends the Visual Composer plugin with a slider on the front page. The slider will show five testimonials from five different customers. I want to add the featured image of each testimonial as the thumbnail in the slider.
Here's the shortened code from the parent theme:
function jo_customers_testimonials_slider( $atts ) {
extract( shortcode_atts( array( 'limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000" ), $atts ) );
$content = "";
$loopArgs = array( "post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1 );
$postsLoop = new WP_Query( $loopArgs );
$content = "";
$content .= '...';
$content .= '...';
$content .= '...';
wp_reset_query();
return $content;
}
add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider' );
My functions.php file:
function jo_customers_testimonials_slider_with_thumbnail( $atts ) {
extract( shortcode_atts( array( 'limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000" ), $atts ) );
$content = "";
$loopArgs = array( "post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1 );
$postsLoop = new WP_Query( $loopArgs );
$content = "";
$content .= '...';
$content .= get_the_post_thumbnail( get_the_ID(), 'thumbnail' );
$content .= '...';
$content .= '...';
wp_reset_query();
return $content;
}
add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail' );
In theory, the function from my functions.php file should overwrite the shortcode from the parent theme. But nothing seems to happen when I use this code. What am I doing wrong?
Edit:
Tried this code, but it still won't work.
function wpa_add_child_shortcodes(){
remove_shortcode('jo_customers_testimonials_slider');
add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail' );
}
add_action( 'after_setup_theme', 'wpa_add_child_shortcodes' );
Also changed
add_action( 'after_setup_theme', 'wpa_add_child_shortcodes' ); to
add_action( 'init', 'wpa_add_child_shortcodes' );
, but no difference in the outcome.
Edit 2 (With Solution):
Changing add_action( 'after_setup_theme', 'wpa_add_child_shortcodes' ); to add_action( 'wp_loaded', 'wpa_add_child_shortcodes' ); solved it.

you need to call remove_shortcode(); like this:
remove_shortcode('jo_customers_testimonials_slider');`
Before you add your new shortcode with the same name to "overwrite" it.
You'll also need to call it after the parent theme has run so we fire on an action hook called wp_loaded.
function overwrite_shortcode() {
function jo_customers_testimonials_slider_with_thumbnail($atts) {
extract(shortcode_atts(array('limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000"), $atts));
$content = "";
$loopArgs = array("post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1);
$postsLoop = new WP_Query($loopArgs);
$content = "";
$content .= '...';
$content .= get_the_post_thumbnail(get_the_ID(), 'thumbnail');
$content .= '...';
$content .= '...';
wp_reset_query();
return $content;
}
remove_shortcode('jo_customers_testimonials_slider');
add_shortcode('jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail');
}
add_action('wp_loaded', 'overwrite_shortcode');

You have to write this code in your Child Theme's functions.php
add_action( 'after_setup_theme', 'calling_child_theme_setup' );
function calling_child_theme_setup() {
remove_shortcode( 'parent_shortcode_function' );
add_shortcode( 'shortcode_name', 'child_shortcode_function' );
}
function child_shortcode_function( $atts) {
$atts = shortcode_atts( array(
'img' => '',
'cat' => '',
'capt' => '',
'link' => ''
), $atts );
//YOUR OWN CODE HERE
$imgSrc = wp_get_attachment_image_src( $atts['img'], 'delicious-gallery' );
$imgFull = wp_get_attachment_image_src( $atts['img'], 'full' );
$b = '<div class="screen-item" data-groups=\'["'.strtolower(str_replace(' ', '_', $atts["cat"])).'", "all"]\'>'.
'<img src="'.$imgSrc[0].'" alt="SCREEN" class="screenImg" />'.
'<span>'.$atts["capt"].'</span>'.
'</div>';
//YOUR OWN CODE HERE
return $b;
}

Related

Show Child Posts of Parent Function Wordpress

I have created a template on wordpress where I want to output the child posts of certain parent posts. It will output 5 child posts to the corresponding parent post.
Parent post
Child Post
Child Post
Child Post
The code for this, looks like this:
function vd_list_child_pages( $atts = array() ) {
$atts = shortcode_atts( array(
'id' => 0,
'slug' => '',
), $atts );
$post_id = absint( $atts['id'] );
$post = get_post( $post_id ); // WP_Post on success.
$childpages = '';
if ( $post && is_post_type_hierarchical( 'page' ) ) {
$childpages = wp_list_pages( array(
'child_of' => $post->ID,
'title_li' => '',
'echo' => 0,
'depth' => 1,
'link_before' => '<i class="far fa-file-alt"></i>',
'walker' => '',
) );
}
if ( $childpages ) {
$childpages = '<ul class="child-posts-content">' . $childpages . '</ul>';
}
return $childpages;
}
add_shortcode( 'vd_childpages', 'vd_list_child_pages' );
However, I need the code in the template more often, with different "post_parent" ID's. Do I have to use the code everywhere like this, or can I create a "function", or which option is correct here?

Remove toolbar from single wp_editor

I have the following code to remove toolbar, media buttons, and visual buttons on my wp_editor. The code is working, but I want it to only remove the items from one wp_editor, not all. Any help is appreciated.
Wp_editor code
$content = '';
$editor_id = 'message';
$settings = array(
'textarea_name' => 'message',
'textarea_rows' => 10,
);
wp_editor( $content, $editor_id, $settings );
Code to hide items
function my_format_TinyMCE( $in ) {
$in['toolbar1'] = '';
$in['toolbar2'] = '';
$in['toolbar'] = false;
return $in;
}
add_filter( 'tiny_mce_before_init', 'my_format_TinyMCE' );
add_filter( 'wp_editor_settings', function($settings) {
$settings['media_buttons']=FALSE;
$settings['quicktags']=FALSE;
return $settings;
});
You can set tinymce setting in editor settings
$content = '';
$editor_id = 'message';
$settings = array(
'textarea_name' => 'message',
'textarea_rows' => 10,
'tinymce' => array(
'toolbar1' => '',
'toolbar2' => '',
'toolbar3' => '',
),
);
wp_editor( $content, $editor_id, $args );

shortcode for related post in wp

I’m using bellow function for related post shortcode and I have a question about it. It’s supposed to display posts with related tag (I don't why it shows just random posts) but I want to use something like [rps tag=google] and the function just return a post has a tag as “google”, I mean nothing related to the current post tags. How can I do that?
This is the code:
add_shortcode('rps', 'fphp_get_related_posts');
function fphp_get_related_posts() {
$reset_post = $post;
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
$post_tag_ids = array();
foreach($post_tags as $post_tag) $post_tag_ids[] = $post_tag->term_id;
$args=array(
'tag__in' => $post_tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page' => 1,
'orderby' => 'rand',
);
$related_query = new wp_query( $args );
if (intval($related_query->post_count) === 0) return '';
$html = '<div class="rps"><ul><h3>Also read:</h3>';
$related_query->the_post();
$html .= '<li style="width:250px">';
$html .= '<div class="relatedthumb"><a rel="external" href="'. get_the_permalink(). '">';
$html .= get_the_title() . '</a>';
$html .= '</div></li>';
}
$post = $reset_post;
wp_reset_query();
$html .= '</ul></div>';
return $html;
}
If you want to show given tag post, then no need to use wp_get_post_tags(), because its return multi tags. try this code,
function fphp_get_related_posts( $atts ) {
$atts = shortcode_atts( [
'tag' => '',
], $atts );
$args = [ 'posts_per_page' => 1, 'tag' => $atts['tag'], 'orderby' => 'rand' ];
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
ob_start(); ?>
<!-- your html here -->
<?php endwhile; endif; wp_reset_postdata();
return ob_get_clean();
}
add_shortcode( 'rps','fphp_get_related_posts' );

add shortcode heading showing multiple time

Hi my code is below for adding shortcode in posts. when i am adding shortcode two times it shows me heading two times that i added in code "Recent Posts" is there is way to show this heading only top means one time?
/*shortcode start*/
add_shortcode( 'recent-posts', 'PL_recent_posts' );
function PL_recent_posts( $atts ) {
extract( shortcode_atts( array(
'numbers' => '5',
'order' => 'ASC',
), $atts ) );
$rposts = new WP_Query( array( 'posts_per_page' => $numbers, 'orderby' => 'date' , 'colorss' => $color ) );
if ( $rposts->have_posts() ) {
$html = '<h3>Recent Posts</h3><ul class="recent-posts">';
while( $rposts->have_posts() ) {
$rposts->the_post();
$html .= sprintf(
'<li>%s</li>',
get_permalink($rposts->post->ID),
get_the_title(),
get_the_title()
);
}
$html .= '</ul>';
}
wp_reset_query();
return $html;
}
Define a global variable to detect whether title is already added.
function PL_recent_posts( $atts ) {
global $title_added;
...
if ( $rposts->have_posts() ) {
if ( $title_added ) {
$html = '<ul class="recent-posts">';
} else {
$html = '<h3>Recent Posts</h3><ul class="recent-posts">';
$title_added = true;
}
Hope that helps..!

Customising excerpt in theme

I've been reading a lot of how to customize excerpt function in WordPress but I have no idea how to proceed with this.
The theme that I am using already have 4 pre-customized excerpt functions and the one that I will show here is closest to my desired but still needs to improve.
My question is how to stop erasing HTML formating from my content (line breaks, paragraphs, font variants, etc)?
add_shortcode('display_news_s5', 'be_display_posts_shortcode5');
function be_display_posts_shortcode5($atts) {
// Pull in shortcode attributes and set defaults
extract( shortcode_atts( array(
'post_type' => 'post',
'post_parent' => false,
'id' => false,
'tag' => '',
'category' => '',
'offset' => 0,
'posts_per_page' => '1',
'order' => 'DESC',
'orderby' => 'date',
'include_date' => false,
'include_excerpt' => false,
'excerpt_l' => 8,
'taxonomy' => false,
'tax_term' => true,
'tax_operator' => 'IN'
), $atts ) );
// Set up initial query for post
$args = array(
'post_type' => explode( ',', $post_type ),
'tag' => $tag,
'category_name' => $category,
'p' => $id,
'posts_per_page' => $posts_per_page,
'order' => $order,
'orderby' => $orderby,
'offset' => $offset
);
// If Post IDs
if( $id ) {
$posts_in = explode( ',', $id );
$args['post__in'] = $posts_in;
}
// If taxonomy attributes, create a taxonomy query
if ( !empty( $taxonomy ) && !empty( $tax_term ) ) {
// Term string to array
$tax_term = explode( ', ', $tax_term );
// Validate operator
if( !in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) )
$tax_operator = 'IN';
$tax_args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $tax_term,
'operator' => $tax_operator
)
)
);
$args = array_merge( $args, $tax_args );
}
// If post parent attribute, set up parent
if( $post_parent ) {
if( 'current' == $post_parent ) {
global $post;
$post_parent = $post->ID;
}
$args['post_parent'] = $post_parent;
}
$listing = new WP_Query( apply_filters( 'display_posts_shortcode_args', $args, $atts ) );
$count = 0;
if ( !$listing->have_posts() )
return apply_filters ('display_posts_shortcode_no_results', false );
$inner = '';
while ( $listing->have_posts() ): $listing->the_post(); global $post;
$count++;
if( $count == 1 ){
$style = ' news-main-post';
} else {
$style = ' news-list-posts';
}
$title = '<div class="news-listing-title"><a class="title" href="'. get_permalink() .'">'. get_the_title() .'</a></div>';
if ($include_date == 'true') $date = ' <div class="news-listing-meta"><span class="news-listing-date">'. get_the_date() . '</span><span class="news-listing-comment">('. get_comments_number() .')</span></div>';
else $date = '';
if ($include_excerpt == 'true') $excerpt = '<span>' .excerpt($excerpt_l) . '</span>';
else $excerpt = '';
$output = '<div class="news-listing' . $style . '"><div class="news-listing-item">'. $title . $excerpt . $date . '</div></div>';
$inner .= apply_filters( 'display_posts_shortcode_output', $output, $atts, $title, $excerpt, $date );
endwhile; wp_reset_query();
$open = apply_filters( 'display_posts_shortcode_wrapper_open', '<div class="news-listing-wrapper-s3">' );
$close = apply_filters( 'display_posts_shortcode_wrapper_close', '<div class="clear"></div></div>' );
$return = $open . $inner . $close;
return $return;
}
Have a look here: LINK looks like its doing what you want to acchieve.

Categories