I have this shortcode code..
function codeThree($attr,$content) {
extract(shortcode_atts(array(
'style' => 'design1',
'title' => 'Here is where the title will be reside..',
'image' => 'codethreeimg'
), $atts));
$dir = get_template_directory_uri();
return '<div class="codethree extend" style="background: transparent url('.$dir.'/images/'.$style.'.jpg) no-repeat top center;"><img class="codethreeimg" src="'.$dir.'/'.$image.'" /><p class="codethreep"><h2 class="codethreetitle">'.$title.'</h2><br/>'.do_shortcode($content).'</p></div>';
}
function register_shortcodes(){
add_shortcode('codethree', 'codeThree');
}
add_action( 'init', 'register_shortcodes');
and this for displaying them..
[codethree style="design1" title="Hi, I'm Armando Gutierrez, #1 Personal Trainer in Torrance & LA, and Body Transformation Specialist." image="http://localhost/wordpress/wp-content/uploads/2013/09/Profile.png"]some contents here[/codethree]
as you can see, this:
'style' => 'design1',
'title' => 'Here is where the title will be reside..',
'image' => 'codethreeimg'
will be default, if user doesn't specify there define content on those array fields, but what happens, the default content was instead displayed although those fields has been filled by the user define content.
[codethree style="design1" title="Hi, I'm Armando Gutierrez, #1 Personal Trainer in Torrance & LA, and Body Transformation Specialist." image="http://localhost/wordpress/wp-content/uploads/2013/09/Profile.png"]some contents here[/codethree]
Could someone check my codes and at least tell me whats wrong please? I'm open to any suggestions, ideas and recommendations! Thank you in advance.
I don't believe you need the add_shortcode to be attached to a hook. Instead of:
function register_shortcodes(){
add_shortcode('codethree', 'codeThree');
}
add_action( 'init', 'register_shortcodes');
Try just add_shortcode('codethree', 'codeThree'); and lose the rest. At least, that's how I've always done mine.
Since you have only one shortcode why creating a function to register? why don't you just use the add_shortcode('codethree', 'codeThree');
here is the example of one of my shortcodes that i do not even to my plugins but to accomplish anything i want this one is just for making a content in a post visible only to the non registered members of the site
add_shortcode('wageni','for_guests_only');
function for_guests_only($atts,$content){
if (!is_user_logged_in()){
return $content;
}
return '';
}
I have added a menu page on my WordPress backend with some submenu items.
A snippet of the code i use is:
// Add to admin_menu function
add_menu_page(__('New Menu'), __('New Menu Title'), 'edit_themes', 'new_menu_item', 'functiontocallonclick', '', 3.5);
// Add to secondlevel menu
add_submenu_page('new_menu_item', __('New |Sub Menu item'), __('New Menu Title item'), 'edit_themes', 'new_menu_sub_item', 'subfunctiontocallonclick',');
As you can see above it is calling the function functiontocallonclick when you go to the New menu item in the backend.
What i am wondering now:
I would like to pass a variable with the function.
functiontocallonclick($value);
Ofcourse it can't be done that way, so what is the good way?
I use this:
switch($_GET['page']){
case 'suppliers': $type='c';
break;
case 'contractors': $type='s';
break;
default: $type='';
break;
}
but I try to find some better solution.
This answer might seem late but I'll share how I work around this because it looks like quite a few people are looking for an answer to this question.
My answer is going to be passing variables to a nested function within a class-based context so if you're using functional programming then disregard the $this.
The Main hook action:
function mainSetup(){
add_action('admin_menu', array($this, 'mainMenuSetup'));
add_action('admin_menu', array($this, 'subMenuSetup'));
}
Menu Setup:
function mainMenuSetup(){
add_menu_page('DashBoard',
'DashBoard',
'manage_options',
'[yoursitesname]-admin-menu',
function(){ $this->pageSelect("admin"); },
'',
200
);
}
Within the example of the Main menu navigation option, we are passed a string in the nested function that will be passed as an argument to the method(function) 'pageSelect', but the string is hardcoded and can't be changed easily. We'll address this problem when setting up the submenu(s).
TODO: You should replace [yoursitename].
SubMenu Setup:
For the submenu let's say we want to make it easier to go back and add new submenus and change who has access to which submenus. For this, I'm going to make an associative array called 'subMenuObjects' where the array's keys are the submenu objects and their values are the page's permissions.
function subMenuSetup(){
$subMenuObjects = array(
'Settings' => 'manage_options',
'Services' => 'manage_options'
);
foreach ($subMenuObjects as $subMenu => $value) {
add_submenu_page('[yoursitesname]-admin-menu',
$subMenu . 'Page',
$subMenu,
$value,
'[yoursitesname]' . strtolower($subMenu) . '-menu',
function() use ($subMenu){ $this->pageSelect($subMenu); }
);
}
}
Here we use the 'use' to pass the $subMenu to the nested function.
Page select:
function pageSelect($page){
switch ($page) {
case 'admin':
echo '<div>Welcome to the Admin page</div>';
break;
case 'Settings':
echo '<div>Welcome to the Settings page</div>';
break;
case 'Services':
echo '<div>Welcome to the Service page</div>';
break;
default:
echo '<div>something happened, contact dev</div>';
break;
}
}
Yes, the way you're doing it is the way WordPress does it itself. To re-use admin screens, you have to pass some query var in the URL and then show/hide elements based on that.
You can also create invisible admin screens: How to enable additional page in WordPress custom plugin?. And this may be useful too: Redirect from add_menu_page
I've been stuck on how to manipulate in the template file what a menu block outputs in it's html. The regular < ul class="menu" > with li links is fine and I don't need to completely gut this html that drupal creates for all menus but I want to inject classes 'inline' and 'links' like the system main menu (usually) already has under it's ul element. I could print the menu directly in the theme skipping blocks altogether but it would be more helpful in the long run to learn injecting class names into the output of menu blocks that are generated.
So far from googling around I've only been able to find a module that can enter ID's and Class names on the individual li's but not the ul wrapping them and I've been unable to get any similar template file snippets I've come across to work.
There is a way to use a hook function to do this isn't there?
Why don't you add the classes you want via javascript?!
Example:
jQuery("#MY_MENU_WRAPPER ul.menu").addClass("inline");
If that's the case, try the following code in your theme's template.php file
function return_menu_markup($menu_name, $attributes)
{
$items = array();
$menu_tree = menu_tree_all_data($menu_name);
$menu_tree_output = menu_tree_output($menu_tree);
foreach($menu_tree_output as $item_id => $item_data)
{
if(is_numeric($item_id) && is_array($item_data))
{
$items[] = l('<span>' . $item_data['#title'] . '</span>', $item_data['#href'], array(
'attributes' => $item_data['#attributes'],
'html' => TRUE,
)
);
}
}
return theme('item_list', array('items' => $items, 'type' => 'ul'));
}
Then anywhere in the template, simply do the following:
$attributes = array();
$attributes['id'] = "MY_MENU_ID";
attributes['class'] = array('MY_CLASS_1', 'MY_CLASS_2', 'MY_CLASS_3');
return_menu_markup("main-menu", $attributes);
Hope you find what needed :)
-Muhammad.
You can use template.php in your theme folder, use hook:
function THEMENAME_menu_tree__menu_main_navigation($variables){
return "<ul class=\"inline\">\n" . $variables['tree'] ."</ul>\n";
}
also note this menu_main_navigation is menu URL path, other values are always same. Do some cache delete few times, maybe it want work immediately.
I am creating a blog for some people that are going to be placing posts on their site within 5 different custom posts that i have created.
As some of the categories require the users to bullet point the post data (therefore creating ul>li ) so ensure it is styled correctly when it appears on the site.
To make sure that the editors of the site input data into the post admin area correctly i wish to either.
Display a image down the right hand side of the new / edit post screen which has instructions on how to create the custom post.
or / and
Have watermarked input forms (like where it sats "insert title here") to give a example on how the data should be displayed.
I am using the plugin more fields to display my input fields. this has the option to insert HTML and I have had mixed success with placing images and code in these as it seems sporadic if it works or not!
Any help is greatly appreciated.
Si
If I understand this correctly, a custom meta box can solve this.
add_action( 'add_meta_boxes', 'wpse_54822_add_custom_box' );
function wpse_54822_add_custom_box()
{
$post_types = array( 'post', 'movies', 'testimonial' );
foreach( $post_types as $box )
{
add_meta_box(
'wpse_54822_sectionid',
__( 'Instructions' ),
'wpse_54822_inner_custom_box',
$box,
'side'
);
}
}
function wpse_54822_inner_custom_box()
{
?><pre>
<b>This text is bold</b>
<strong>This text is strong</strong>
<i>This text is italic</i>
<em>This text is emphasized</em>
<code>This is computer output</code></pre>
<?php
}
Which results in:
Other possibility could be inserting a custom Help Tab.
I'm building a WordPress plugin and I'd like to have an edit-item page that can't be reached via the submenu (because then the item wouldn't be specified).
This resource (http://codex.wordpress.org/Adding_Administration_Menus) shows how to associate an admin page with a function, but not how to do so without adding it as a menu item.
Can this be done?
Thanks!
Best solution here http://wordpress.org/support/topic/add-backend-page-without-menu-item
use add_submenu_page with parent slug = null
I have finally discovered a way to do this that isn't an ugly hack, doesn't require JS to highlight the desired menu item (and submenu item), and works for regular menus registered by plugins (#Josh's answer only works for custom post types).
Essentially, you just need to register your submenu normally, but then hook into the 'submenu_file' filter to deregister it and optionally also set another submenu item to highlight instead.
function so3902760_wp_admin_menu() {
// Register the parent menu.
add_menu_page(
__( 'Parent title', 'textdomain' )
, __( 'Parent', 'textdomain' )
, 'manage_options'
, 'my_parent_slug'
, 'display_my_menu'
);
// Register the hidden submenu.
add_submenu_page(
'my_parent_slug' // Use the parent slug as usual.
, __( 'Page title', 'textdomain' )
, ''
, 'manage_options'
, 'my_hidden_submenu'
, 'display_my_submenu'
);
}
add_action( 'admin_menu', 'so3902760_wp_admin_menu' );
function so3902760_wp_admin_submenu_filter( $submenu_file ) {
global $plugin_page;
$hidden_submenus = array(
'my_hidden_submenu' => true,
);
// Select another submenu item to highlight (optional).
if ( $plugin_page && isset( $hidden_submenus[ $plugin_page ] ) ) {
$submenu_file = 'submenu_to_highlight';
}
// Hide the submenu.
foreach ( $hidden_submenus as $submenu => $unused ) {
remove_submenu_page( 'my_parent_slug', $submenu );
}
return $submenu_file;
}
add_filter( 'submenu_file', 'so3902760_wp_admin_submenu_filter' );
Yes, this can be done (well, technically, it would be more like registering the whole thing and then removing the menu item later), but It would just be easiest (I think) to check for parameters in the $_GET super-global to indicate that the user wishes to edit a specific item.
For example, you could have a page that lists items to edit, and clicking 'edit' only adds the item's ID to the current URL(query-string).
In the function that displays this page, if ID is defined, give them the page to edit that item.
Otherwise, give them the list view. That's how posts, pages, and other custom post types do it.
add_submenu_page with parent slug = null
OR
add_submenu_page with menu title = null
use this code for creating new page without adding in menu
add_action( 'admin_menu', 'register_newpage' );
function register_newpage(){
add_menu_page($appname, $appname, 'administrator','custompage', 'custom');
remove_menu_page('custom');
}
function custom()
{
echo "hai";
}
Note: This solution doesn't automatically set the current menu and submenu item. If you want to highlight a particular menu as current when the hidden page is viewed, see my other answer.
From the answers that come before me, you can see that there are many ways to do this. However, there is another way that I think may be the best.
Loading the page differently based on the value of a $_GET query var is one option, but it may not be what some people are looking for.
The suggestions regarding add_submenu_page() are on the right track, but each of the previous suggestions have problems. Setting $menu_title to null doesn't keep the menu item from being displayed, it just makes it so the link doesn't have any text. The link still takes up some room in the menu though, so it looks funny. Setting the $parent_slug to null doesn't have this problem, but I noticed that the page's HTML title doesn't display the $page_title text.
My solution was to set $parent_slug to a fake menu slug, like 'i_dont_exist'. The menu item won't be displayed, and when viewing the admin screen the page title will be filled out properly.
add_submenu_page(
'_doesnt_exist'
,__( 'Page title', 'textdomain' )
,''
,'manage_options'
,'menu_slug'
,'display_my_menu'
);
Yes. It is very possible to make a page cannot be reach via submenu, or even the main menu in the WP admin panel. See the code snippet below.
function myplugin_render_edit_page() {
// Code contains the UI for edit page.
}
/**
* Manage menu items and pages.
*/
function myplugin_register_admin_page() {
global $_registered_pages;
$menu_slug = plugin_basename('myplugin.php');
$hookname = get_plugin_page_hookname($menu_slug,'');
if (!empty($hookname)) {
add_action($hookname, 'myplugin_render_edit_page');
}
$_registered_pages[$hookname] = true;
}
add_action('admin_menu', 'myplugin_register_admin_page');
Hopefully, this will help.
Create sub menu page and parent slug leave it empty like this:
// Create page were you can add new users.
public function add_create_user_menu() {
add_submenu_page(
'',
'Create User',
'Create User',
'manage_options',
'create-user',
array( $this, 'add_create_user_page' )
);
}
You can access it like this:
Add New
I've tried all of the suggestions here but with various issues associated with each.
The WordPress codex for add_submenu_page now gives the correct answer, which is to use options.php as your parent slug. I tried the trick of using a made up name but that gives permissions errors, equally use of null at various locations either causes the menu text to simply be missing (but still clickable) or for the browser title to go missing.
Using options.php worked and I've not seen any issues as a result of its use.
Using add_submenu_page with a parent of NULL definitely works, however if you want to keep the non-linked page associated with a particular menu (say a custom post type menu), you have to use a variation of #Boopathi's answer:
function my_hidden_submenu_page(){
//add the submenu page the usual way
add_submenu_page('edit.php?post_type=custom-type', 'My Page Title', 'My Page Title', 'manage_options', 'my-page-slug', 'my_page_callback');
//then remove it
remove_submenu_page('edit.php?post_type=custom-type','my-page-slug');
}
add_action('admin_menu', 'my_hidden_submenu_page');
It looks as though the two actions would cancel each other out, however remove_submenu_page does not unregister the callback function; it merely removes the link.
This way when someone is viewing your non-linked page, the correct navigation menu (our custom post type menu in this example) will still show as active.
One of the problems I found with merely adding null as the parent slug for a sub menu item is that if you're currently viewing that specific page the submenu itself won't display (at least it didn't for me (along with the page title not showing).
What I did was add an empty span element inside the menu title and use jquery to traverse the parent elements and hide it.
It seems this need is still valid for nowadays version.
I am using WordPress 5.3.2 and I used the following methods to remove the parent named menu from the submenu.
add_action( 'admin_menu', 'submenus' );
function submenus()
{
global $submenu;
$parent_slug = 'parent_slug_name';
// remove parent named menu from submenu because it is always the first one in the submenu array, so the offset is 0 and remove just 1
array_splice( $submenu[$parent_slug], 0, 1 ); // with reindex
// or unset( $submenu[$parent_slug][0] ); // without reindex
}
Hope it helps others who want to achieve the same goal.
About php array_splice()
this method could be found in the source of WP function remove_submenu_page() which is available since WP 3.1.0
Edit / Added
Apart from submenu, parent menu could also be updated in similar way.
For parent menu, the global variable is $menu.
example for reference:
add_action( 'admin_menu', array( $this, 'modify_menu_title' ) );
function modify_menu_title() {
global $menu;
$page = 'some-page-slug';
$new_menu_title = 'New Title Name';
foreach( $menu as $key => $value ) {
if( $menu[$key][2] === $page ) {
$menu[$key][0] = $new_menu_title;
}
}
}
I find you can do it by reusing the insert id, like so:
add_menu_page( 'User AS Packages', 'User AS', 'manage_options', 'myplugin/editaspackages.php', 'UserASPackages', '', 8);
add_menu_page( 'User ARP Packages', 'User ARP', 'manage_options', 'myplugin/editarppackages.php', 'UserARPPackages', '', 8);
add_menu_page( 'AS Packages', 'AS Packages', 'manage_options', 'myplugin/ars-s2.php', 'ARPPackages', '', 8);
The last 3 using position 8 and the last one overrides the two before so the two before do not appear.