My wordpress plugin wont be added to the left-side menu - wordpress

I have built a very simple plugin and uploaded it on my wordpress account which is run on localhost. Once I activate it, its output will be shown on every page, and it wont be added to the left hand side menu of wordpress. what should I add to it to be added to the menu? I suppose it should be done using add_action but do not know how !!
<?php
/*
Plugin Name: myphotos Plugin
Description: A simple wordpress plugin.
Version: 1.0
Author: Saeed Pirdost
Copyright: 2012, Saeed Pirdost
*/
?>
<?php
add_filter('admin_notices','myprint');
?>
function myprint()
{
echo "hello";
}
?>
I used the following code as well but when I activate the plugin just a white page will be shown.
add_menu_page(__('My Menu Page'), __('My Menu'), 'edit_themes', 'my_new_menu', 'myprint', '', 7);

First, stop opening and closing PHP tags at every line, you don't need that. This is used when some HTML is happening in the middle of your PHP.
Also, the last closing ?> of a PHP file can/should be omitted. It may even break a site if there is a white-space after it...
Refer to WordPress_Coding_Standards.
Second, you need to decide if you want to use the function my_print as callback of admin_notices or of add_menu_page. Can it be both? In this case, yes, but really, do it only if you know what you are doing.
Your plugin is breaking because add_menu_page cannot be called directly.
Always check the documentation of each function that don't work as expected:
Function_Reference/add_menu_page
Here's a working version of your plugin:
<?php
/*
Plugin Name: myphotos Plugin
Description: A simple wordpress plugin.
Version: 1.0
Author: Saeed Pirdost
Copyright: 2012, Saeed Pirdost
*/
add_filter( 'admin_notices', 'print_my_notice' );
add_action( 'admin_menu', 'register_custom_menu_page' );
function print_my_notice()
{
echo '<div class="updated">
<p>I am a big notice that appears everywhere</p>
</div>';
}
function register_custom_menu_page()
{
add_menu_page(
__( 'My Menu Page', 'my-plugin-text-domain' ),
__( 'My Menu', 'my-plugin-text-domain' ),
'edit_themes',
'my_new_menu',
'print_my_menu',
'http://upload.wikimedia.org/wikipedia/commons/a/aa/Wink%2816px%29.svg',
7
);
}
function print_my_menu()
{
echo '<div id="icon-post" class="icon32"></div>
<h2>My plugin</h2>
<p><a class="button-secondary" href="#"> '
. __( 'I am a translation', 'my-plugin-text-domain' )
. '</a>
</p>';
}

Open your wordpress Admin Area
Click on appearance->widgets
Drag and drop the Menu to desired place
Or
Click Appearance -> Menu
and click on sidebar menu
and add items there
Hope it helps

Related

How do I assign wordpress plugin to menu?

wp-content/plugins/myplugin.php
how to assign this plugin to top level menu? E.g. menu on my website: about us contacts, services my plugin. I'm using add_menu_page but I dont know why its not working.
I have used it, when prepare my theme. You may used it also. Put it at functions.php
add_action( 'admin_menu', 'register_YOURPLUGINNAME_menu_page' );
function register_YOURPLUGINNAME_menu_page(){
add_menu_page( 'PLUGIN Info', 'PLUGINNAME', 'manage_options', 'custompage', 'YOURPLUGIN_menu_page', 'dashicons-tickets' , 10 );
}
function YOURPLUGIN_menu_page(){
?>
<h1>TEST YOUR MIGHT :)
<?php
}
?>

How to add menu item on dashboard menu in wordpress

I am trying the following syntax to add the custom menu on dashboard. and I put this code in 'function.php' in theme folder file. but its not displaying menu on dashboard. So, please help me in which file I put this code? otherwise any other solution please help me.
<?php
add_action( 'admin_menu', 'register_my_custom_menu_page' );
function register_my_custom_menu_page(){
add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'custompage', 'my_custom_menu_page', plugins_url( 'myplugin/images/icon.png' ), 6 );
}
function my_custom_menu_page(){
echo "Admin Page Test";
}
?>
In which function.php file are you writing this code? are you writing in the core files? if you are writing in the theme's function file just make sure that the theme is currently active.

Create a top level menu in wordpress admin for a child theme

I am creating a dedicated theme for an app with some custom settings that will be used just a few times to set up the app from a fresh wordpress install.
How do I create a top level menu in wordpress admin for a twenty twelve child theme via functions.php and a page to contain the settings? I would like to have it as the first item in the menu.
Here's a solution. Just put this code in the functions.php
<?php
/******* New menu item for admin *********/
add_action( 'admin_menu', 'register_my_custom_menu_page' );
function register_my_custom_menu_page(){
add_menu_page( 'custom menu title', '♥Custom theme options♥', 'manage_options', 'custompage', 'my_custom_menu_page', '' /* or something like ... plugins_url( 'myplugin/images/icon.png' )*/, 1 );
}
function my_custom_menu_page(){
?>
Hello world. This is a simple example page.
<?php
}
?>

How to display page title of menu / sub menu page?

I would like to know what the usage of $page_title in function add_menu_page or add_submenu_page because I don't see it in the menu / sub menu page.
To have the title, I have to manually code the plugin, such as :
<div class="wrap">
<h2>Page Title</h2>
....
Here is the code for creating menu / sub menu :
add_menu_page(
'Menu Page Title',
'Menu Title',
'publish_posts',
'otto-ext' );
add_submenu_page(
'otto-ext',
'Sub Menu Page Title',
'Sub Menu Title',
'publish_posts',
'location-ext',
'custom_menu' );
Is it possible to display "Menu Page Title" or "Sub Menu Page Title" automatically?
Thanks in advance
I know I'm two years late to the party, but for those still landing on this page:
You can use get_admin_page_title(), which has been available since 2.5. Alternatively, you can just use $GLOBALS['title'], which WordPress sets as soon as admin-header.php is loaded.
<div class="wrap">
<h2><?php echo $GLOBALS['title'] ?></h2>
<!-- ... -->
</div>
You are correct. You will have to code the title manually. The $page_title parameter is only for the title tag in the head section of the HTML document, <title>$page_title</title>, which is used as the "name" of the tab in your browser.
There is no way to use the parameters given to add_menu_page inside the function that echoes the content of the page you're creating. So no, there is no easy way to generate page titles automatically from that information.
Here is an example of a menu and sub-menu using scripts:
add_action( 'admin_menu', 'MyPluginMenu' );
function MyPluginMenu() {
$SettingsPath = 'path_to_plugin/menu_script.php';
add_menu_page( 'PageTitle', 'MenuTitle', 'administrator', $SettingsPath, '', plugins_url( 'path_to_plugin/images/plugin_icon.png' ) );
add_submenu_page( $SettingsPath, 'PageTitle', 'SubMenuTitle', 'administrator', 'path_to_plugin/sub_menu_script.php', '' );
}
This will work for PLUGINS as you mentioned in your question I have to manually code the plugin, such as:.

How do I sort by a custom field without manually creating a new page?

Having a bit of an issue with Wordpress here. In all honesty I've always designed my sites from scratch and "coded" from the ground up. Lately I've been trying to work with WP as I've heard good things about it.
It would appear that WP gives you many things for free (e.g. dynamic "pages" based on CATEGORIES). However, I would like to know how to manipulate these freebies without reinventing the wheel. For example, I would like to have my SUB-MENU display a list of post categories. But I would like to sort those categories by a CUSTOM FIELD.
Now, I could reinvent the wheel and manually create (and link to) a new page for each sort, so on and so forth, (which I don't fundamentally mind doing) however, I'm hoping there is a way around this via plugins or otherwise. I've seen several tutorials on custom queries, but they stop short of implementation -- they simply give the query without telling exactly whether to create a new page or plug it into a function somewhere.
Any input would be most appreciated.
Best.
At the top of category.php template in your theme's root directory, add the following to add your custom sort field to the query:
<?php
function is_valid_custom_sort_field($field)
{
// implementation left as an exercise for the questioner
return true;
}
if ($_REQUEST['sort_custom_field'] && is_valid_custom_sort_field($_REQUEST['sort_custom_field'])) {
query_posts($query_string . '&orderby='.$_REQUEST['sort_custom_field']);
}
See:
http://codex.wordpress.org/Function_Reference/query_posts
If your theme doesn't have a category.php, here is a simple default template to base it on (copied from the included twentyten theme):
<?php
/**
* The template for displaying Category Archive pages.
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<h1 class="page-title"><?php
printf( __( 'Category Archives: %s', 'twentyten' ), '<span>' . single_cat_title( '', false ) . '</span>' );
?></h1>
<?php
$category_description = category_description();
if ( ! empty( $category_description ) )
echo '<div class="archive-meta">' . $category_description . '</div>';
/* Run the loop for the category page to output the posts.
* If you want to overload this in a child theme then include a file
* called loop-category.php and that will be used instead.
*/
get_template_part( 'loop', 'category' );
?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Resources