Custom shortcode always displays at top of page - php

I realise there are similar questions to this on here, but all contain slightly different code to mine. I have a custom shortcode configured in the functions.php file, and for some reason it always displays at the top of the page. Nothing I do seems to be able to move it. Is this a result of something in the code?
function menu_shortcode( $atts ) {
return wp_nav_menu( array( 'theme_location' => 'header' ) );
}
add_shortcode( 'nav', 'menu_shortcode' );

Your shortcode is returning wp_nav_menu() but that function will output by default. Shortcodes aren't allowed to generate output so you need to set echo to false to prevent it.
function menu_shortcode( $atts ) {
return wp_nav_menu( array(
'theme_location' => 'header',
'echo' => false,
) );
}
add_shortcode( 'nav', 'menu_shortcode' );
Documentation:
https://developer.wordpress.org/reference/functions/wp_nav_menu/#parameters
https://codex.wordpress.org/Function_Reference/add_shortcode
The answer you received to your original question has been updated to fix the problem you've described: How to make a PHP function into a short code

All right, I've tested it on my own WordPress install with the following code:
function menu_shortcode() {
return wp_nav_menu( array( 'theme_location' => 'header' ) );
}
add_shortcode( 'b3_add_menu', 'menu_shortcode' );
I created a page called test and added the shortcode [b3_add_menu] to it. This is the result:
For me the menu is not displayed only at the top of the page, could you maybe tell me where you placed your "nav" shortcode?

Related

How would you do this basic wordpress plugin?

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

Creating custom tab with ultimatemember plugin of wordpress

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

wordpress shortcode attributes not being passed

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.

wp_nav_menu only prints menu if I don't include the location (which was registered)

I was able to register a new menu location (it shows in the admin Appearance->Menus section. But when I call wp_nav_menu with that location, it prints nothing. If I set location =>'' then it prints it!
functions.php
//This works
add_action( 'init', 'register_my_menus' );
function register_my_menus()
{
register_nav_menus( array( 'my_special_slug' => __( 'My Menu' )));
}
header.php
wp_nav_menu(
array(
'items_wrap'=> '%3$s',
'walker' => new MyWalker(),
'container'=>false,
'menu_class' => '',
'theme_location'=>'my_special_slug', //This does NOT work
'fallback_cb'=>false
)
);
What am I doing wrong?
register_nav_menu takes a comma separated array.
function register_my_menus()
{
register_nav_menu(array( 'my_special_slug', 'My Menu');
}
It appears (please correct me if this is incorrect) that without the user going into the admin interface and turning the menu on, it cannot be found. They need to go to `Appearance->Menus->Locations" and select the menu to show for that location.
I have been unable to find a programmatic way of doing this.

Wordpress Navigation Help

I am working on a site that calls the categories of a wordpress page and displays them in the right-side navigation using a php call. I am new to php and web programming in general. Is there a way I could split the categories into two sections using a particular php call or perhaps an if-loop.
Essentially, I want to display particular categories under custom sub-headings to better organize the site. Any help, I'm currently using the following script to display my categories:
<ul><?php wp_list_categories('show_count=1&title_li='); ?></ul>
Here is my site for reference: http://www.merrimentdesign.com
Try using your code above twice. Each time, you can use the other function arguments to limit the output to certain categories. See http://codex.wordpress.org/Template_Tags/wp_list_categories for the various ways to customize the output of the function.
For example you could use:
<ul><?php wp_list_categories('show_count=1&title_li=&child_of=100'); ?></ul>
// where 100 is the parent id of all of the categories you want to print.
<ul><?php wp_list_categories('show_count=1&title_li=&exclude_tree=100'); ?></ul>
// and then show everything, but children of 100
Or simply use the first string multiple times specifying different parent ids each time.
By far and away your best option is to use the new menu functionality within WordPress. It's dead straight forward to set up in your theme:
add_theme_support( 'menus' );
add_action( 'init', 'register_my_menus' );
function register_my_menus() {
register_nav_menus(
array(
'public-menu' => __( 'Public Menu' ),
'sidebar-public-menu' => __( 'Sidebar Public Menu' ),
'sidebar-members-menu' => __( 'Sidebar Members Menu' ),
'sidebar-staff-menu' => __( 'Sidebar Staff Menu' ),
'footer-menu' => __( 'Footer Menu' )
)
);
}
place that in your functions.php file (and obviously change it for your requirements).
Then in your template file - probably sidebar.php you'll want something like:
<?php wp_nav_menu( array( 'theme_location' => 'sidebar-staff-menu', 'container' => false ) ); ?>
And then go to the back end of WordPress (your wp-admin) and then go to Appearance > Menus and voila you're able to drag and drop your categories to your heart's content!
Helpful link: http://justintadlock.com/archives/2010/06/01/goodbye-headaches-hello-menus
Read that, Justin Tadlock is awesome.
Good luck.

Categories