modify shortcode atts(), is that possible? - php

i use link shortcode on my template functions.php like this,
function post_link_shortcode( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'id' => '',
'kelime' => '',
), $atts )
);
// Code
if ( isset( $id ) ) {
return '<a target="_blank" rel="nofollow" href="' . $id . '"> <strong>' .$kelime . '</strong></a>';
}
}
add_shortcode( 'link', 'post_link_shortcode' );
Usage :
[link id="someurl.com" kelime="link title"]
could we change this like;
[link|someurl.com|link title]
is that possible ? than how ? thanks.

Related

How to overwrite "wc_display_item_meta" function in WooCommerce

I am looking to have an action included in my functions.php file that changes the echo array from true to false in the below code:
function wc_display_item_meta( $item, $args = array() ) {
$strings = array();
$html = '';
$args = wp_parse_args(
$args,
array(
'before' => '<ul class="wc-item-meta"><li>',
'after' => '</li></ul>',
'separator' => '</li><li>',
'echo' => true,
'autop' => false,
'label_before' => '<strong class="wc-item-meta-label">',
'label_after' => ':</strong> ',
)
);
The above code is found in the WooCommerce wc-template-functions.php file found on line 3273.
How can I do this?
In includes/wc-template-functions.php, you will see that the function in there is wrapped in a if ( ! function_exists( 'wc_display_item_meta' ) ) { conditional.
So redefine the same function in functions.php file of the active child theme (or active theme) and your custom function will override the existing.
Note: A function can only be reassigned this way once.
So you get:
/**
* Display item meta data.
*
* #since 3.0.0
* #param WC_Order_Item $item Order Item.
* #param array $args Arguments.
* #return string|void
*/
function wc_display_item_meta( $item, $args = array() ) {
$strings = array();
$html = '';
$args = wp_parse_args(
$args,
array(
'before' => '<ul class="wc-item-meta"><li>',
'after' => '</li></ul>',
'separator' => '</li><li>',
'echo' => false,
'autop' => false,
'label_before' => '<strong class="wc-item-meta-label">',
'label_after' => ':</strong> ',
)
);
foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) {
$value = $args['autop'] ? wp_kses_post( $meta->display_value ) : wp_kses_post( make_clickable( trim( $meta->display_value ) ) );
$strings[] = $args['label_before'] . wp_kses_post( $meta->display_key ) . $args['label_after'] . $value;
}
if ( $strings ) {
$html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
}
$html = apply_filters( 'woocommerce_display_item_meta', $html, $item, $args );
if ( $args['echo'] ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $html;
} else {
return $html;
}
}

Wordpress - How to specify an ID in custom Visual Composer element

I'm current building a website for a non-profit, and am using an older edition of the Wordpress theme, Savior.
There is a custom post type of Donations, and it allows the admin to set up different "Causes", each of which is a post with a specific ID.
By default, the theme has a custom Visual Composer element (Screenshot 1 - https://i.stack.imgur.com/cmhTu.png) that allows you to feature the most recent "Cause"; the only customization option you have is for the Title of the VC element.
I'm trying to update this custom VC element so that the admin can specify the exact ID for the "Cause" they would like to feature on a page/post vs. only showing the latest "Cause."
I've adjusted the VC mapping for the cause.php template where commented below:
<?php
class STM_VC_Causes {
function __construct() {
add_action( 'init', array( $this, 'integrateWithVC' ) );
add_shortcode( 'stm_causes', array( $this, 'render' ) );
}
public function integrateWithVC() {
if ( function_exists( 'vc_map' ) ) {
vc_map( array(
'name' => __( 'Causes', STM_DOMAIN ),
'base' => 'stm_causes',
'category' => __( 'STM', STM_DOMAIN ),
'params' => array(
array(
'type' => 'textfield',
'class' => '',
'heading' => __( 'Title', STM_DOMAIN ),
'param_name' => 'title',
'value' => __( 'Our Causes', STM_DOMAIN )
),
/** ========================================
* Qing Custom 8-6-2018
* Allow admin to select Cause to feature
======================================== **/
array(
'type' => 'textfield',
'class' => '',
'heading' => __( 'Cause ID', STM_DOMAIN ),
'param_name' => 'id',
'value' => __( '', STM_DOMAIN )
)
)
) );
}
}
public function render( $atts, $content = null ) {
/** ========================================
* Qing Custom 8-6-2018
* Display selected Cause ID
======================================== **/
$title = '';
$id = '';
extract( shortcode_atts( array(
'title' => '',
'id' => ''
), $atts ) );
$fea_cause = $atts['id'];
$donations = new WP_Query( array( 'post_type' => 'donation', 'posts_per_page' => 1, 'post__in' => $fea_cause ) );
$output = '';
$output .= '<ul class="donation-grid first clearfix">';
while ( $donations->have_posts() ) {
$donations->the_post();
$target_amount = ( get_post_meta( get_the_ID(), 'donation_target', true ) == '' ) ? 0 : get_post_meta( get_the_ID(), 'donation_target', true );
$raised_amount = ( get_post_meta( get_the_ID(), 'donation_raised', true ) == '' ) ? 0 : get_post_meta( get_the_ID(), 'donation_raised', true );
$currency = ( get_post_meta( get_the_ID(), 'donation_currency', true ) == '' ) ? '$' : get_post_meta( get_the_ID(), 'donation_currency', true );
$donors = ( get_post_meta( get_the_ID(), 'donation_donors', true ) == '' ) ? 0 : get_post_meta( get_the_ID(), 'donation_donors', true );
$target_amount_percent = ( $raised_amount / $target_amount ) * 100;
$output .= '<li id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">';
$output .= '<div class="donation-thumbnail">';
$output .= '<a href="' . get_the_permalink() . '">';
if ( has_post_thumbnail() ) {
$output .= get_the_post_thumbnail( get_the_ID(), 'thumb-150x150' );
}
$output .= '</a>';
$output .= '</div>';
$output .= '<div class="donation-content">';
$output .= '<h4>' . get_the_title() . '</h4>';
$output .= '<div class="progress_bar"><span style="width: ' . $target_amount_percent . '%;"></span></div>';
$output .= '<div class="donation-stat">';
$output .= '<span><i class="fa fa-child"></i> ' . __( 'Raised', STM_DOMAIN ) . '<br/>' . $currency . $raised_amount . '</span>';
$output .= '<span><i class="fa fa-users"></i> ' . __( 'Donors', STM_DOMAIN ) . '<br/>' . $donors . '</span>';
$output .= '<span><i class="fa fa-thumbs-up"></i> ' . __( 'Goal', STM_DOMAIN ) . '<br/>' . $currency . $target_amount . '</span>';
$output .= '</div>';
$output .= '<div class="donate_now">';
$output .= '' . __( 'DONATE NOW', STM_DOMAIN ) . '';
$output .= '</div>';
$output .= '</div>';
$output .= '</li>';
}
$output .= '</ul>';
wp_reset_query();
return $output;
}
}
if( defined( 'WPB_VC_VERSION' ) ){
new STM_VC_Causes();
}
?>
The custom VC element is showing up correctly for me on the admin backend side of the site (Screenshot 2 - https://i.stack.imgur.com/zKCgs.png), but I cannot figure out how to get the admin-inputted ID to show up on the frontend - no matter what, it still shows the most recent "Cause." Screenshot 3 (https://i.stack.imgur.com/13Qw4.png) is simply an example screenshot of what the individual "Cause" looks like when a page with the custom VC element is pushed live.
I've contacted the support team for the theme, but they only suggested this Post Types Order WP plugin, which only allows you to change the displayed "Cause" across the entire site, instead of allowing you to specify it in a page-by-page/post-by-post basis. I've also scoured Google/StackOverflow and tried various queries in the WP Codex, building out a custom shortcode (the custom VC element itself is a custom shortcode: [stm_causes]), but it just displays the most recent "Cause."
Edit 8/7/18:
I've made several edits to the causes.php template within the Savior theme, but for some reason, the updated WP_Query loop ended up not pulling in any data (Screenshot 3: https://i.stack.imgur.com/JLibO.png ).
The only exception is if I omit any ID in the VC editor backend; If I don't enter an ID, it defaults to the most recent Cause. However, if I enter any ID, even if it's the same ID as the most recent post, nothing shows up...
Any idea what could be wrong with my logic?
Thank you!
render function Fixed!
$fea_cause = $atts['id'];
$donations = new WP_Query( array( 'post_type' => 'donation', 'posts_per_page' => 1, 'post__in' => array($fea_cause )));
Huge props to Nilesh Sanura for his help!

Trying to call a widget using a shortcode in a page but the function doesn't work

I am trying to use a function I found to call widgets on the wysiwyg editor on WordPress, however it doesn't seem to be working.
the short code is:
[wysiwyg_widget id="sidebar"]
The function itself is:
function wysiwyg_widget( $atts ) {
global $wp_widget_factory;
extract( shortcode_atts( array(
'widget_name' => "WYSIWYG_Widgets_Widget",
'id' => null,
), $atts ) );
if ( ! is_a( $wp_widget_factory->widgets[ $widget_name ], 'WP_Widget' ) ) :
$wp_class = 'WP_Widget_' . ucwords( strtolower( $class ) );
if ( ! is_a ( $wp_widget_factory->widgets[ $wp_class ], 'WP_Widget' ) ) :
return '<p>' .sprintf( __( "%s: Widget class not found. Make sure this widget exists and the class name is correct" ), '<strong>' . $class . '</strong>' ) . '</p>';
else:
$class = $wp_class;
endif;
endif;
if ( ! is_null( $id ) ) {
$instance['wysiwyg-widget-id'] = $id;
}
ob_start();
the_widget( $widget_name, $instance, array(
'widget_id'=>'arbitrary-instance-'.$id,
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => ''
) );
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode( 'wysiwyg_widget','wysiwyg_widget' );
Any suggestions or if there is another way to do it without a plugin? Thank you for helping in advance!

Adding conditional div class to Genesis shortcode entry-header

I am desperately trying to find a solution to add a conditional div class to individual categories when using the Genesis shortcode [post_categories] in the entry-header.
I want to apply unique colors to category titles in the entry header for articles on the home page and on individual posts. www.mic.com has this affect. You will notice that articles under the "World" category have the category text one color, while articles under the "News" category have the category text displayed as a different color.
For example, if the category is "Infrastructure", I'd like there to be a class that wraps around the existing entry-categories class.
I want it to look like this:
<div class="infrastructure section"><span class="entry-categories"></span></div>
If the category is "Elections", I'd like it to show:
<div class="elections section"><span class="entry-categories"></span></div>
The code I need to edit to get this effect is listed below.
add_shortcode( 'post_categories', 'genesis_post_categories_shortcode' );
/**
* Produces the category links list.
*
* Supported shortcode attributes are:
* after (output after link, default is empty string),
* before (output before link, default is 'Tagged With: '),
* sep (separator string between tags, default is ', ').
*
* Output passes through 'genesis_post_categories_shortcode' filter before returning.
*
* #since 1.1.0
*
* #param array|string $atts Shortcode attributes. Empty string if no attributes.
* #return string Shortcode output
*/
function genesis_post_categories_shortcode( $atts ) {
$defaults = array(
'sep' => ', ',
'before' => __( 'Filed Under: ', 'genesis' ),
'after' => '',
);
$atts = shortcode_atts( $defaults, $atts, 'post_categories' );
$cats = get_the_category_list( trim( $atts['sep'] ) . ' ' );
if ( genesis_html5() )
$output = sprintf( '<span %s>', genesis_attr( 'entry-categories' ) ) . $atts['before'] . $cats . $atts['after'] . '</span>';
else
$output = '<span class="categories">' . $atts['before'] . $cats . $atts['after'] . '</span>';
return apply_filters( 'genesis_post_categories_shortcode', $output, $atts );
}
I have tried using the get_the_category_list function to conditionally create the class names, but the command is not completing an action. When viewing my site through firebug, the new class shows the command as text.
$cats = get_the_category_list( trim( $atts['sep'] ) . ' ' );
if ( genesis_html5() )
$output = sprintf( '<div class="<?php echo $cats[0]->cat_name; ?> section"><span %s>', genesis_attr( 'entry-categories' ) ) . $atts['before'] . $cats . $atts['after'] . '</span></div>';
Any ideas on how to achieve this affect?
Thanks so much!
Something like this should work ( but only for html5 ):
function custom_terms_shortcode( $atts ) {
$defaults = array(
'after' => '',
'before' => __( 'Filed Under: ', 'genesis' ),
'sep' => ', ',
'taxonomy' => 'category',
);
$atts = shortcode_atts( $defaults, $atts, 'custom_terms' );
$terms = get_the_terms( get_the_ID(), $atts['taxonomy'] );
if ( is_wp_error( $terms ) )
return;
if ( empty( $terms ) )
return;
if ( genesis_html5() ){
$output .= '<span class="entry-terms">';
$output .= $atts['before'];
foreach ($terms as $term) {
$output = '<div class="'. $term->name .'">';
$output .= ''.$term->name.''.$atts['sep'];
$output .= '</div>';
}
$output = substr($output, 0, -2);
$output .= $atts['after'];
$output .= '</span>';
}
return apply_filters( 'genesis_post_terms_shortcode', $output, $terms, $atts );
}

If attribute's value is one thing, echo another in shortcode

Here's a shortcode of mine:
function sc_link( $atts ) {
extract( shortcode_atts(
array(
'page' => '',
'style' => 'button',
'window' => 'self',
'label' => 'Missing Label Tag: label=""',
), $atts )
);
return '' . $label . '';
}
add_shortcode( 'mylink', 'sc_link' );
What I want to be able to do is a conditional before the return: if $window = 'new' then echo 'blank'.
Think this is what your trying to do what id suggest doing is leaving window blank and just doing an if statement on that
How you want it
function sc_link( $atts ) {
extract( shortcode_atts(
array(
'page' => '',
'style' => 'button',
'window' => 'self',
'label' => 'Missing Label Tag: label=""',
), $atts )
);
if($window == "new"){
$link = '' . $label . '';
}else{
$link = '' . $label . '';
}
return $link;
}
add_shortcode( 'mylink', 'sc_link' );
How it should be
function sc_link( $atts ) {
extract( shortcode_atts(
array(
'page' => '',
'style' => 'button',
'window' => '',
'label' => 'Missing Label Tag: label=""',
), $atts )
);
if(!$window){
$link = '' . $label . '';
}else{
$link = '' . $label . '';
}
return $link;
}
add_shortcode( 'mylink', 'sc_link' );
The your shortcode will be link
[shorcode page="" window="" label=""]
Here is an easy short code conditional output example. I would recommend being sparing with this type of syntax because it can get lost in the fray.
echo ($window === 'new') ? $window : '';
This format is conditional ? what to do if true : what to do if false

Categories