I've created a short code that I'm trying to pass an attribute into, but I don't seem to be receiving the value on the other end.
Here's what I've got below;
function list_display($atts) {
extract( shortcode_atts(
array(
'pageName' => 'No Page Received',
), $atts )
);
echo $pageName;
add_shortcode('list-briefings-display', 'list_display');
}
and the shortcode being used is
[list-display pageName="My Page Name"]
and I'm running a require_once from functions.php
require_once ( TEMPLATEPATH. '/includes/list-display.php' );
But what I'm seeing on the screen is 'No Page Received', can anyone think of something I might've missed?
There is more content being generated by the shortcode, which I have't included, that's rendering fine. So it just seems to be something to do with how I've passed the attribute.
Any help is appreciated.
function list_display($atts) {
$atts = shortcode_atts( array(
'pagename' => 'No Page Received'
), $atts );
extract($atts);
echo $pagename;
}
add_shortcode('list-display', 'list_display');
You'll probably want to use "return" instead of "echo" if you're using the shortcode within pages and posts.. Echo could cause it to send output to the screen a little too early and won't end up exactly where you may be expecting it to be.
There was also a little formatting issue in your code that I've corrected, mainly trying to use add_shortcode() from within the very same function you're trying to reference. I also changed the first parameter of add_shortcode() to the shortcode you were trying to use in your example.
Related
I'm starting to develop it and I realise it's kind of tricky.
I need to convert the shortcode
[helloworld] into <h1>"Hello World"</h1>
and be able to change
"Hello World" from admin
Can someone point me on the right way?
You need create hellowrold as a shortcode.
function hellowrold_func( $atts ) {
$a = shortcode_atts( array(
'content' => 'Default content',
), $atts );
return "<h1>{$a['content']}</h1>";
}
add_shortcode( 'hellowrold', 'hellowrold_func' );
Then you can use it like this and change text as you like from the content attribute.
[hellowrold content="Hello World"]
See Shortcode API for more details.
If you need to add this to a plugin, simply create a file with a header content like this:
/**
* Plugin Name: YOUR PLUGIN NAME
*/
Add the code above below this header and put the file inside a folder in your plugins directory.
See Plugins Basics for more details.
For such kind of purpose, you need to use shortcode_atts (https://developer.wordpress.org/reference/functions/shortcode_atts/).
Simply define the below function
function torque_hello_world_shortcode( $atts ) {
$a = shortcode_atts( array(
'title' => 'Heading'
), $atts );
return '<h1>"' . $a["title"] . '"</h1> from admin';
}
To use the above function, simply add the below shortcode .....
1. [helloworld title="Hello World"]
returns
"Hello World" from admin
2. [helloworld title="Hi"]
returns
"Hi" from admin
On my wordpress page (which I will use as a template to duplicate) I need to use a custom field 'dropbox' as an href url.
I created a PHP Code Snippet (shortcode) to use as a replacement for the url: [xyz-ips snippet="dropbox"]
Here is the PHP within the shortcode:
<?php the_field('dropbox'); ?>
Here is the code on the page:
download the documents
This short code will not pass the url from custom field to the href. Any thoughts?
Thanks in advance.
Your shortcode should be working if it's as you describe, but you should note shortcodes need to return values, not echo them. You didn't post the code you're using to register it, so I can only assume you're echoing content instead of returning it.
With that said,
I would just make the shortcode echo out the whole entire link, that way you have a bit more granular control over the whole thing:
add_shortcode( 'xyz-ips-link', function( $atts ){
extract( shortcode_atts( array(
'snippet' => 'dropbox',
'text' => 'Download the Documents',
'target' => '_new',
), $atts ) );
$link = sprintf( '%s', get_field( $snippet ), $target, $text );
return $link;
});
This will let you just use [xyz-ips-link] anywhere in your content,
or <?php echo do_shortcode( '[xyz-ips-link]' ); ?> in your page templates.
It also gives you more granular control over the content of the link, such as [xyz-ips-link snippet="dropbox" text="Some Alternative Text"].
You'll also note I'm using get_field() instead of the_field(). WordPress (and ACF) both have get_ functions that return a variable for you to use, and the_ functions which get and output the variable by default.
You can see an example of this code here, Note:I don't have ACF installed, so I replaced the href attribute)
Hi Please try it may work for you .
echo do_shortcode('dropbox');
Try it
$dropdown = get_the_field('dropbox');
echo do_shortcode($dropbox);
OR
$dropdown = get_field('dropbox');
echo do_shortcode($dropbox);
So it turns out there is a specific way to call url custom fields (ACF). I inserted the below code into a PHP Code shortcode. This worked like a charm!
<?php
$link = get_field('dropbox');
if( $link ): ?>
<a class="button" href="<?php echo $link; ?>" target="_new">download documents</a>
<?php endif; ?>
I am using Event Manager plugin with Advanced Custom Fields plugin In WordPress.
In my events post type, I have added an image gallery with ACF and want to display the first image in the events list page created by Events Manager. The only way I can do that within the events list is by creating a shortcode that will read the serialized array returned from the custom field which is usually something like a:1:{i:0;s:4:"6903";}.
function unseralLink( $atts ) {
$atts = shortcode_atts(
array(
'id' => '',
), $atts, 'unseralizeLink');
if($atts['id']!='')
{
$mydata = unserialize($atts['id']);
$url = wp_get_attachment_image_url($mydata[0]);
return "<img src=\"".$url."\" alt=\"\" class=\"attachment-thumbnail size-thumbnail\" />";
}
}
add_shortcode( 'unseralizeLink', 'unseralLink' );
I call the shortcode with [unseralizeLink id="#_ATT{gallery}"]. But nothing gets returned.
What I really need help with is reading the serialized array as a shortcode argument and storing it to a local variable within the function. After that, I should be ok.
After a lot of experimenting I found the solution.
function eventImgURL( $atts ) {
shortcode_atts(
array(
'id' => '',
), $atts );
$myvar = unserialize($atts[id]);
return wp_get_attachment_image($myvar[0]);
}
add_shortcode( 'unseralizelink', 'eventImgURL' );
Part of the issue was that I was calling the serialized array using double quotes in the shortcode when double quotes were used.
[unseralizelink id='#_ATT{gallery}']
I just had the same problem and the solution the asker posted wouldn't work. Here's mine, working nicely, for the next one having the same problem:
function eventImgURL( $atts ) {
$atts = shortcode_atts( array( 'id' => '',), $atts, 'unserialize-link' );
return wp_get_attachment_image($atts['id']);
}
add_shortcode( 'unserialize-link', 'eventImgURL' );
Then call it in your HTML template as follows:
<p>[unserialize-link id='#_ATT{logo}']</p>
See the documentation on shortcodes from the WordPress Codex for more information.
Working on a Wordpress site and where I need to have a post with php content.
I figured out that this is only possible with a plugin or a shortcode in the functions.php
Googled around, tried a lot but it isn't working out, so i definitely doing something wrong.
code I have in functions.php:
function anniversary_link($text) {
$url = the_permalink();
return "<a href='$url'>$text</a>";
}
add_shortcode('permalink', 'anniversary_link');
And the post I have:
and the result I get when clicking the link:
The shortcode has to reference to the single.php page and al the static code's references to the single.php page is just:
<?php the_permalink() ;?>
Is this te 'correct' way to use href on a post (is there a better/cleaner way to get this working)?
Edit
Updated my code thanks to nathan Edit edit: in functions.php
function anniversary_link( $atts ) {
$atts = shortcode_atts( array(
'text' => '',
), $atts, 'permalink' );
$url = get_permalink();
return '' . $atts['text'] . '';
}
add_shortcode('permalink', 'anniversary_link');
And how I use this short code inside a post (I think that I incorrectly use the shortcode):
Result:
Edit Edit
This is how I call the dynamic anniversary post:
<?php echo get_posts(array( 'category_name' => 'Anniversary' ))[0]->post_content ;?>
(inside the header)
solution thanks to nathan
Reading through the code you've posted I see three issues.
The way you're accessing the 'text' attribute.
The function you're using to get the permalink.
The way you're inserting your shortcode into your content.
Shortcode Attributes
The first parameter of a shortcode callback should be an array of attributes, not a single string. Naming your parameter $text has no bearing on the value and won't pull the text attribute of your shortcode.
Change the name of your parameter from $text to $atts and set a default value for the text attribute. Setting a default value is a good practice with shortcodes and can be done using the shortcode_atts() function.
Return vs. Output Functions
The second issue is your use of the_permalink(). the_permalink() doesn't return a permalink but outputs it directly instead. As such you can't assign it to a variable.
The new function
function anniversary_link( $atts ) {
// Set defaults where needed
$atts = shortcode_atts( array(
'text' => '',
), $atts, 'permalink' );
// Replace the_permalink().
// Given the level of simplicity it doesn't need it's own variable.
$url = get_permalink();
// Put together a new return statement.
// Various ways this could be formatted. I went with something clear and easy to understand.
return '' . $atts['text'] . '';
}
Usage
In your code you're using the shortcode inside the href attribute of a link. The shortcode returns a full link, not a URL, and therefore shouldn't be inside another a tag.
Example:
[permalink text="My Link Text"]
// Outputs My Link Text
I'm trying to created a custom shortcode in wordpress but I can't get it working.
Here's the code I have so far:
function wp_test_video($atts) {
extract(shortcode_atts(array(
'X' => ''
'Y' => ''
), $atts));
return '[iframe src="http://www.example.com/test.php?X='.$atts['X'].'&Y='.$atts['Y'].'"]';
}
add_shortcode('test', 'wp_test_video');
Everytime I try and insert it into my functions.php file my site just breaks.
EDIT: The shortcode is now working but it seems to be acting differently from the same code inserted without the shortcode.
Here is an image of the compiled code from the shortcode in a post by itself:
http://i.imgur.com/4lM8jC5.jpg
Here is what a post looks like when using the shortcode to generate the same code:
http://i.imgur.com/gxk6k3C.jpg
The video is embedding but it's breaking out of the article wrapper causing none of widgets or comments to work (also appears to mess up the search bar at the top).
You forgot a comma after your first item in your array and forgot to assign the shortcode_atts to a variable. Don't use extract(), it's deprecated.
function wp_test_video($atts) {
$atts = shortcode_atts(
array(
'X' => '', // <-- This one
'Y' => ''
),
$atts);
return '[iframe src="http://www.example.com/test.php?X='.$atts['X'].'&Y='.$atts['Y'].'"]';
}