I have a shortcode for a CSS accordion. One of the attributes is $open for the checkbox's "checked" value. Right now, if they type open=checked, it will pass checked="checked" and that div will be visible by default. Is there anyway to replace a custom value with the actual html value? For instance, I want the user to be able to use "open=yes" and have it pass "checked=checked" in the HTML.
// TOGGLE BOXES
function sc_tog_boxes( $attr, $content = null ) {
return '<ul class="togboxes">' . $content . '</ul>';
}
add_shortcode('togboxes', 'sc_tog_boxes');
function sc_tog_box( $atts , $content = null ) {
// Attributes
extract( shortcode_atts(
array(
'number' => '1',
'heading' => 'you forgot the heading tag: heading="your heading"',
'open' => '',
), $atts )
);
// Code
return '<li><label for="' . $number . '">' . $heading . ' <span class="toggle-icon">[+/-]</span></label><input type="checkbox" name="a" id="' . $number . '" checked="' . $open . '"><div class="togbox">' . do_shortcode($content) . '</div></li>';
}
add_shortcode( 'togbox', 'sc_tog_box' );
UPDATE: Okay, I need to rephrase this because the presence of the "checked" attribute means "checked" without any parameter anyway. So I need to be able to add "checked" in the html if they just type "open" in the shortcode, but have no checked attribute if they don't type open.
Found the answer:
if($open === 'yes'){
$open = 'checked';
}
else {
$open = '';
}
Related
I'm working on a shortcode to show my authors list on WordPress; I managed to make the social networks fields work, but I can't hide them, if the field is empty.
I imagine I need the if statement, but I can't seem to find a way to make it works.
Here is the code I am currently using:
function pippin_list_authors() {
$authors = get_users(array(
'orderby' => 'display_name',
'count_totals' => false,
'who' => 'authors',
'id' => ''
)
);
$list = '';
if($authors) :
$list .= '<div class="author-list">';
foreach($authors as $author) :
$list .= '<table>';
$archive_url = get_author_posts_url($author->ID);
$list .= '<tr>' . get_avatar($author->user_email, 100);
$list .= '<br><br><a class="author-list-name" href="'. $archive_url . '" title="' . __('Vedi tutti i post ', 'pippin') . $author->display_name . '">' . $author->display_name . '</a><br>';
$list.= '<br><br><div class="author-social"><i class="fa fa-facebook"><p style="font-size: 0; color: transparent;">'.A.'</p></i>';
$list .= '<i class="fa fa-twitter"><p style="font-size: 0; color: transparent;">'.A.'</p></i>';
$list .= '<p class="author-bio" style="max-height: 103px; overflow: hidden; ">' . get_user_meta($author->ID, 'description', true) . '</p>';
$list .= '<div class="author-archive" style="text-align: center;">' . __('Vedi tutti i post', 'pippin') . '</div>';
$list .= '</tr></table>';
endforeach;
$list .= '</div>';
endif;
return $list;
}
add_shortcode('authors', 'pippin_list_authors');
If you want to check if the variable $authors is empty use the empty() function.
Try this:
if(!empty($authors)) {
...
}
I would suggest you change your functions if possible.
Create function for every social link and other user metas like this:
$facebook_link = get_user_meta( $author->ID, 'facebook', true);
then call it in this way:
<?php if ($facebook_link !=='') { ?>
<p><i class="fa fa-facebook"></p>
<?php } ?>
If get_user_meta exist in DB, but isn't value, it still return no empty array so empty($our_meta) - will not help.
Bcz return array( [0] => string("") );
Empty string, but this empty value is in the array, so it will show
that the array is not empty.
Use get_user_meta($id, $key, true) it return '' and empty() will work as it should.
get_user_meta( $user_id, $key, $single );
$single(boolean) If set to true, the function will return the value of the meta field, if left false, then the value of the meta
field will be returned in an array. This parameter has no meaning if
the $key parameter has not been specified.
Default: false
- If $single = true
string/array - if the meta field exists.
'' - when the metafield does not exist.
If $single = false
an array of all metafield values - when the metafield exists.
array() - when the meta field does not exist.
I have been attempting to create my own shortcode for my Wordpress theme to make life a bit easier. Unfortunately I have run into issues with my code.
Below, the code in the Javascript section is what I have placed in my functions.php file, and in the HTML section is what I have placed on my page in Wordpress as the shortcode, however nothing seems to appear?
1: JAVASCRIPT
2: HTML
function hire_equipment_func($atts) {
extract( shortcode_atts( array(
'img' => 'no-img',
'user' => 'no-user',
'text' => 'no-text',
), $atts ) );
if ( $atts['img'] == '' || $atts['user'] == '' || $atts['text'] == '' ) return;
$output =
'<div>
<div>' . $atts['img'] . '</div><br />
<div>' . $atts['user'] . '</div>
<div>' . $atts['text'] . '</div>
</div>';
return $output;
}
add_shortcode( 'hire_equipment', 'hire_equipment_func' );
[hire_equipment img="" user="Test Float" text="Can be used anywhere"]
Any help with this issue would be GREATLY appreciated!!
Kind regards,
Jesse
This will return nothing if one of the attributes is not set, and will return each attribute in a div otherwise.
function hire_equipment_func($atts) {
$atts = array_change_key_case((array)$atts, CASE_LOWER);
if (!isset($atts['img']) || !isset($atts['user']) || !isset($atts['text'])) return;
$output =
'<div>
<div>' . $atts['img'] . '</div><br />
<div>' . $atts['user'] . '</div>
<div>' . $atts['text'] . '</div>
</div>';
return $output;
}
add_shortcode( 'hire_equipment', 'hire_equipment_func' );
function hire_equipment_func($atts) {
extract( shortcode_atts( array(
'img' => 'no-img',
'user' => 'no-user',
'text' => 'no-text',
), $atts ) );
if ( $atts['img'] == 'no-img' || $atts['user'] == 'no-user' || $atts['text'] == 'no-text' ) return;
$output =
'<div>
<div>' . $atts['img'] . '</div><br />
<div>' . $atts['user'] . '</div>
<div>' . $atts['text'] . '</div>
</div>';
return $output;
}
add_shortcode( 'hire_equipment', 'hire_equipment_func' );
[hire_equipment img="" user="Test Float" text="Can be used anywhere"]
Okay, for some reason I placed the same text from each attribute (e.g. 'no-img' from attribute 'img') into each text area in the IF statement, and it worked!
So I have answered my own question! However, I am still interested in wondering WHY that works the way it did. I am willing to mark the question as ANSWERED once someone helps me in understanding the concept reason behind it.
Thanks!
Jesse
This will remove sections that are not specified, so if you toss this snippet of PHP into your functions.php file (in your child theme, preferably):
function hire_equipment_func( $atts ) {
extract(
shortcode_atts(
array(
'img' => 'no-img'
, 'user' => 'no-user'
, 'text' => 'no-text'
)
, $atts
)
);
$output = '<div>' . PHP_EOL;
if( isset( $atts[ 'img' ] ) ) { $output .= ' <div>' . $atts[ 'img' ] . '</div><br>' . PHP_EOL; }
if( isset( $atts[ 'user' ] ) ) { $output .= ' <div>' . $atts[ 'user' ] . '</div>' . PHP_EOL; }
if( isset( $atts[ 'text' ] ) ) { $output .= ' <div>' . $atts[ 'text' ] . '</div>' . PHP_EOL; }
$output .= '</div>';
return $output;
}
add_shortcode( 'hire_equipment', 'hire_equipment_func' );
and place this WordPress shortcode on your page:
[hire_equipment img="" user="Test Float" text="Can be used anywhere"]
you will get:
Test Float
Can be used anywhere
Example 2:
[hire_equipment img="/file/path/image.svg" user="Test Float" text="Can be used anywhere"]
yields
/file/path/image.svg
Test Float
Can be used anywhere
Example 3:
[hire_equipment img="/file/path/image.svg" text="Can be used anywhere"]
outputs
/file/path/image.svg
Can be used anywhere
Example 4:
[hire_equipment img="" user="" text=""]
shows
caused by that line break in your markup.
If you're wondering, PHP_EOL is a PHP constant denoting the correct 'End Of Line' symbol for your platform.
I have a single contact form the runs through the footer of every page on my site.
I have got the page title to echo out using wp_title but it is appearing before the form rather than in the string.
$html .= sprintf( '<form onsubmit="ga(\'send\', \'event\', \'newsletter\', \'submitted\', \'' . wp_title('') . '\')" %s>', $atts ) . "\n";
It appears like this:
From documentation:
Your code
wp_title( string $sep = '»', bool $display = true, string $seplocation = '' )
secod parameter determines if the title will be echoed or returned, in Your case you should set $display parameter to false.
Solution:
$html .= sprintf( '<form onsubmit="ga(\'send\', \'event\', \'newsletter\', \'submitted\', \'' . wp_title('', false) . '\')" %s>', $atts ) . "\n";
I have an shortcode that i would like it to pass an different class once a certain attribute is added to the shortcode. How do you do that? or what is the best way to do this?
Shortcode:
function one_half_columns($atts, $content = null){
$type = shortcode_atts( array(
'default' => 'col-md-6',
'push' => 'col-xs-6'
), $atts );
return '<div class="' . $type['push'] . '">' . do_shortcode($content) . '</div>';;
}
add_shortcode('one_half', 'one_half_columns');
Example when wordpress user enter [one_half type="push"] i want it to use the value of push in array col-xs-6.
You're example has a couple of problems - you are passing an argument of "type" in your shortcode, but then expecting arguments of "default" and "push" in your shortcode. What you want to do is assign the results of shortcode_atts() to $atts and then use an if statement or switch case on $atts['type'];
function one_half_columns($atts, $content = null){
// populate $atts with defaults
$atts = shortcode_atts( array(
'type' => 'default'
), $atts );
// check the value of $atts['type'] to set $cssClass
switch( $atts['type'] ){
case 'push':
$cssClass = 'col-xs-6';
break;
default:
$cssClass = 'col-md-6';
break;
}
return '<div class="' . $cssClass . '">' . do_shortcode($content) . '</div>';
}
add_shortcode( 'one_half', 'one_half_columns' );
Now when you call:
[one_half type="push"]my content[/one_half]
You should get the output:
<div class="col-xs-6">my content</div>
I added a content rating system to my platform where the authors can select which audience their post is appropriate for. Currently, these options are available:
Unrated
G
PG
R
The code that I use to display the rating options on the post edit page is:
// Article Content Rating
add_action( 'add_meta_boxes', 'rating_select_box' );
function rating_select_box() {
add_meta_box(
'rating_select_box', // id, used as the html id att
__( 'Content Rating (optional)' ), // meta box title
'rating_select_cb', // callback function, spits out the content
'post', // post type or page. This adds to posts only
'side', // context, where on the screen
'low' // priority, where should this go in the context
);
}
function rating_select_cb( $post ) {
global $wpdb;
$value = get_post_meta($post->ID, 'rating', true);
echo '<div class="misc-pub-section misc-pub-section-last"><span id="timestamp"><label>Article Content Rating: </label>';
$ratings = array(
1 => ' G ',
2 => ' PG ',
3 => ' R ',
);
echo '<select name="rating">';
echo '<option value=""' . ((($value == '') || !isset($ratings[$value])) ? ' selected="selected"' : '') . '> Unrated </option>';
// output each rating as an option
foreach ($ratings as $id => $text) {
echo '<option value="' . $id . '"' . (($value == $id) ? ' selected="selected"' : '') . '">' . $text. '</option>';
}
echo '</select>';
echo '</span></div>';
}
add_action( 'save_post', 'save_metadata');
function save_metadata($postid)
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
if ( !current_user_can( 'edit_page', $postid ) ) return false;
if( empty($postid) ) return false;
if ( is_null($_REQUEST["rating"]) ) {
delete_post_meta($postid, 'rating');
} else {
update_post_meta($postid, 'rating', $_REQUEST['rating']);
}
}
// END Article Content Rating
Now, the problem is, what code do I add to single.php to display their choice? So for instance, if the author selected PG, then I want to echo 'Content Rating: PG'; or if it was on default (unrated), I want to echo 'Content Rating: Unrated';. How is this possible? Ideally, a solution that is light on the server as my platform is heavily trafficked.
You already use the delete_post_meta() and update_post_meta() functions to remove and modify the custom values. Simply use the get_post_meta() function to obtain the value for the current post, and echo it out as you see fit.
If you're in The Loop, it would be something like:
$rating = get_post_meta(get_the_ID(), 'rating', TRUE);
Edit:
You already know your ID to rating mapping (I might make this mapping a global array, or some defines, or something similar). Simply use that to look up the string to output:
$ratings = array(
1 => 'G',
2 => 'PG',
3 => 'R'
);
if(array_key_exists($rating, $ratings)) {
echo "Content Rating: $ratings[$rating]";
} else {
echo "Content Rating: Unrated";
}
It looks as if you're saving the key to your custom field rather than its value. I suppose this would be alright if you planned on redeclaring your $ratings array, or (God forbid) use it globally. But still, it would probably be best if you saved the actual Rating rather than its ID number.
So change this line:
foreach ($ratings as $id => $text) {
echo '<option value="' . $id . '"' . (($value == $id) ? ' selected="selected"' : '') . '">' . $text. '</option>';
}
To this:
echo '<option value="Unrated"' . ((!$value || $value == 'Unrated' ? ' selected="selected"' : '') . '">Unrated</option>';
foreach ($ratings as $id => $text) {
echo '<option value="' .trim($text). '"' . (($value == trim($text)) ? ' selected="selected"' : '') . '">' . $text. '</option>';
}
After setting your value within the post, you can add this in single.php:
if(have_posts()) : while(have_posts()) : the_post();
echo 'Content Rating: '.get_post_meta($post->ID, 'rating', true);
endwhile;endif;
UPDATE:
As mentioned in my comments, I personally try to avoid globals and redeclaring things as much as possible. So if you would prefer to still reference your ratings by Key, you can get around globals and redeclaring your ratings array by adding a few simple functions:
functions.php
function gw_get_ratings_array()
{
$ratings = array(
1 => 'G',
2 => 'PG',
3 => 'R'
);
return $ratings;
}
function gw_get_rating($key=0)
{
$i = (int)$key;
$ratings = gw_get_ratings_array();
return isset($ratings[$i]) && $ratings[$i] ? $ratings[$i] : 'Unrated';
}
single.php
if(have_posts()) : while(have_posts()) : the_post();
$rating_id = get_post_meta($post->ID, 'rating', true);
echo 'Content Rating: '.gw_get_rating($rating_id);
endwhile;endif;
This way, if you ever need to add more Rating Types, you only need to alter the gw_get_ratings_array function rather than searching for each declaration of the array itself.