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
Related
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.
I've been trying to create a custom tab for my website and am using UltimateMember plugin.
After bit of google I found some code snippet that can help me do it:
First we need to extend main profile tabs
add_filter('um_profile_tabs', 'add_custom_profile_tab', 1000 );
function add_custom_profile_tab( $tabs ) {
$tabs['mycustomtab'] = array(
'name' => 'My custom tab',
'icon' => 'um-faicon-comments',
);
return $tabs;
}
Then we just have to add content to that tab using this action
add_action('um_profile_content_mycustomtab_default', 'um_profile_content_mycustomtab_default');
function um_profile_content_mycustomtab_default( $args ) {
echo 'Hello world!';
}
But my question, to what file should I add this code to achieve what I need. It sounds very numb of me to ask this, but I'm seriously confused.
Thanks for any help.
Let me share my similar experience. First of all in this code :
$tabs['mycustomtab'] = array(
'name' => 'My custom tab',
'icon' => 'um-faicon-comments',
);
You should use always
mycustomtab
as key which I see you've already used. So that's true.
Generally it works out when you put this code in your active theme's functions.php
But if it doesn't work out, consider adding this to core file um-filters-misc.php in the plugin core file folder for ultimate-member. Let me know if it works for you.
Not sure if anyone still needs help on this, but make sure to add a 'custom' key/value like this:
$tabs['mycustomtab'] = array(
'name' => 'My custom tab',
'icon' => 'um-faicon-comments',
'custom' => true // <- needs to be added so it shows up on the profile page
);
The code example in the UltimateMember documentation -specifically for extending the Profile Menu using hooks- doesn't work because the sample code doesn't have that line.
Hi guys thanks for your help... im using this codes and works "good"... if a want to put shotcode the website print "blue screen" error... [ultimatemember form_id="15817"] (if i use "hello word" works perfect..
function um_mycustomtab_add_tab( $tabs ) {
$tabs['mycustomtab'] = array(
'name' => 'Seguimiento',
'icon' => 'um-faicon-pencil',
);
return $tabs;
}
add_filter( 'um_profile_tabs', 'um_mycustomtab_add_tab', 1000 );
// Action
function um_profile_content_mycustomtab_default( $args ) {
echo do_shortcode('[ultimatemember form_id="15817"]'); //this not work
}
add_action( 'um_profile_content_mycustomtab_default', 'um_profile_content_mycustomtab_default');
here some screenshots tests
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'].'"]';
}
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.