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'].'"]';
}
Related
I have made my first plugin and it all works on the front end but I am having some weird backend conflict with the gutenberg. I have done two things, made my plugin automatically add the pages / overwrite them when they've checked off, and made the plugin shortcode.
Here is the problem on my gutenberg:
As you can see there are random red diamonds on the page. When I use the inspector I can see it has code from my plugin I created...
Here is how I created my plugin
First inside my functions page I registered a new short code.
add_shortcode('parts_listing_portal', 'portal_parts_list');
function portal_parts_list(){
include "shortcodes/parts_listing_shortcode.php";
return ;
}
Then I made content for inside the shortcode using JS, PHP, and HTML.(this is a big file with includes and API calls)
After the page looked the way I would like, I created a new function to work on my option submit. This function creates the page with the shortcode inside.
//Add parts
if ( get_option( 'parts' ) === "checked") {
$parts_list = array(
'post_title' => 'Parts',
'post_type' => 'page',
'post_name' => 'Parts',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '[parts_listing_portal]',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0
);
// PARTS LIST
if(get_page_by_title('Parts') == null) {
wp_insert_post($parts_list);
}else{
$post_exists = get_page_by_title("Parts" );
$update_post = array('ID' => $post_exists->ID);
$post_update = wp_parse_args($parts_list, $update_post);
wp_update_post($post_update);
}
When my function adds to the page & you go to the gutenberg editor it shows the full page for a split second then goes away. I feel I missed a statement somewhere to only show on "page" but I have no idea where to look. I've spent a lot of time researching it but couldn't find it.
Thank you
Whenever adding a shortcode make sure to check iif in admin area. If in admin area return nothing
function portal_parts_list(){
if ( is_admin()){
return;
}else {
include "shortcodes/parts_listing_shortcode.php";
return ;
}
}
add_shortcode('part_description_portal', 'portal_parts_list');
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'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.
I have a plugin on my wordpress site that allows me to create team members. I am putting code into my author.php file of my child theme to display the 'Our Team' plugin content on the author archive page. The plugin instructs hard coding the template like so (look at usage examples). I need to use the slug argument to get the author's 'Our Team' information; this is what I'm doing:
In author.php in my child theme:
<?php
do_action( 'woothemes_our_team', array(
'slug' => the_author_meta('user_nicename'), // This line is the problem
'display_author_archive' => false,
)
);
?>
You can see how I want to use the returned value of the the_author_meta() function as the value for the slug key in the array. However, this does not return the correct result. Instead I get the slug output to the template and every team member name listed underneath that. Using the_author_meta('user_nicename') outputs the correct info when I take it out of the array. Assigning it to a variable did not work either:
<?php
$test = the_author_meta('user_nicename');
do_action( 'woothemes_our_team', array(
'slug' => $test,
'display_author_archive' => false,
)
);
?>
Hard coding the slug makes the function work the intended way:
<?php
$test = 'cleo-g';
do_action( 'woothemes_our_team', array(
'slug' => $test,
'display_author_archive' => false,
)
);
?>
The above will output the correct 'Our Team' plugin content to the template. Why is it that when the the_author_meta('user_nicename') function outputs the correct data it will not work inside the array as a key value, or even if I assign it to a variable?
You are using the wrong function. See the Note at http://codex.wordpress.org/Function_Reference/the_author_meta:
NOTE: Use get_the_author_meta() if you need to return (not display) the information.