Wordpress admin menu - wordpress

I tried hundrad of times to load a custom page when top level menu items are clicked.
add_menu_page('Hotels', 'Hotels', 'administrator', 8, 'main_menu_foo');
Where should I put my "view-hotels.php" file?
I want to built a menu as
[Hotels]
[Hotels] //points to view-hotel.php
[Add new...] // points to add-hotel.php

Add menu page needs a call back function to render the page. You can wrap your page in a function and add with an include or you could include the page using your callback function. Also 'administrator' is not a capability. Heres an example:
add_action( 'admin_menu', 'add_admin_menus' )
function add_admin_menus() {
add_menu_page( 'Hotels', 'Hotels', 'manage_options', call_back_function );
}
call_back_function() { ?>
<div class="wrap">
<?php wp_nonce_field( 'hotels' );
hotels_page_function(); ?>
</wrap>
<?php }
include_once( '/view-hotels.php' );

Related

Redirect with notice after form submission

I create a sub menu page and there is a form on it. The data is processed by admin-post.php.
I use wp_safe_redirect to redirect back to the previous page after the form is submitted. Now I'm trying the admin_notices hook to display a message to inform user by showing a message after the redirect. But the notice won't show, what am I doing wrong?
add_action( 'admin_post_hidden_value_of_form', 'setting_response' );
function setting_updated(){
?>
<div class="notice notice-success is-dismissible">
<p>Saved successfully!</p>
</div>
<?php
}
function setting_response() {
wp_safe_redirect( admin_url( 'admin.php?page=plugin-setting-url' ) );
exit;
add_action( 'admin_notices', 'setting_updated' );
}

Create custom wordpress admin submenu item from an existing custom menu item that opens an iframe

Not sure how to layout the question sorry if it was confusing.
I have already used the below code to add a custom link on the admin menu and it opens up an iframe to a map I have created with a plugin.
How can I add a submenu item to this? I grabbed this code from another site and did not create it myself.
add_action( 'admin_menu', 'oa_register_menu_document' );
function oa_register_menu_document() {
add_menu_page( 'Manual', 'Producers Map', 'manage_options', 'oa-document', 'oa_view_document_page',3);
}
function oa_view_document_page(){
?>
<div id="poststuff" class="oa-document">
<div class="postbox hide-if-js" id="postexcerpt" style="display: block; height: 100vh;">
<iframe src="https://www.mywebsite.com/producers" width="100%" height="100%"></iframe>
</div>
</div>
<?php
}
Top-level menu pages are created using the add_menu_page() function, as seen in the code you posted. To create a submenu page, use the related function add_submenu_page().
Example:
function oa_register_menu_document() {
add_menu_page( 'Manual', 'Producers Map', 'manage_options', 'oa-document', 'oa_view_document_page', 3);
add_submenu_page( 'oa-document', 'My Submenu Page', 'My Submenu Page', 'manage_options', 'oa-document-submenu-page', 'oa_submenu_page' );
}
add_action( 'admin_menu', 'oa_register_menu_document' );
function oa_submenu_page() {
// Output your submenu page content...
}
Documentation: https://developer.wordpress.org/reference/functions/add_submenu_page/

Product page details tabs to lightbox

in my woocomere product page there is tabs of a product to display different information (description, etc). But i wish to be able to in a specific tab turn in a ligthbox popup that shows text or a image. Is there any plugin available in wordpress for it?
Please try below code in function.php to show tab content in light box. Might be you will have to customize more but basic structure i am sharing with you.
add_action( 'woocommerce_product_write_panel_tabs','outputTabTitle');
add_action( 'woocommerce_product_write_panels','outputTabEditContent');
add_filter( 'woocommerce_product_tabs','productTab');
function outputTabTitle ()
{
?>
<li class="custom_tab">
Additional Information
</li>
<?php
}
function outputTabEditContent ()
{
global $woocommerce, $post;
echo '<div id="custom-tab" class="panel woocommerce_options_panel">';
/************Please put you lightbox text and image here.
echo '</div>';
}
function productTab ( $tabs )
{
$tabs['custom-tab'] = array(
'title' => __( 'Additional Information', 'woocommerce' ),
'priority' => 50,
'callback' => 'outputTabEditContent'
);
return $tabs;
}

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
}
?>

Virtual Page Within Theme Template

I am creating a plugin that needs a virtual page in order to dynamically create a "Review Order" page (Orders are a custom post type I created). I was able to create a virtual page using the following code:
// Set up the rewrite rules for the order page
add_action( 'init', 'action_init_redirect' );
function action_init_redirect() {
add_rewrite_rule( 'orders/?', 'index.php?orders=new', 'top' );
}
add_filter( 'query_vars', 'filter_query_vars' );
function filter_query_vars( $query_vars ) {
$query_vars[] = 'orders';
return $query_vars;
}
add_action( 'parse_request', 'action_parse_request');
function action_parse_request( &$wp ) {
if ( array_key_exists( 'orders', $wp->query_vars ) ) {
//Beginning of page code
echo "hello";
exit;
}
}
The problem is that this creates a page with a blank template, that is, the above code creates a blank page with the text hello. I would like for the virtual page to be within the theme of the site and displayed like a regular page within the WordPress framework. How to accomplish this?
A solution is to add a template page inside parse_request:
function action_parse_request( $wp ) {
if ( array_key_exists( 'virtual', $wp->query_vars ) ) {
get_header(); ?>
<div id="primary">
<div id="content" role="main">
Hello, world!
</div>
</div>
<?php get_footer();
exit;
}
}
try using the following:
define('WP_USE_THEMES', true);
require('/ABSOLUTE_PATH_HERE/wp-blog-header.php');
require('/ABSOLUTE_PATH_HERE/wp-load.php');
you will need to add this to the top of your page.

Resources