wordpress subpage tag - wordpress

I'm building a wordpress theme right now and I'm using conditionals in the sidebar to display different information for each page like so:
if (is_page('services')) {
etc.....
} elseif (etc....
However, some of the subpages do not have their own specific conditional. How can I make it so that a conditional for a parent page applies to its subpages as well?
Thanks!

You could do this a number of ways. There might be an easier way, but this is what I would do:
On the page itself, check if it is a child page:
if ( is_page() && $post->post_parent )
{
// This is a subpage
return true;
}
else
{
// This is not a subpage
return false;
}
which will check if the page is a child, in which case you can fetch the conditional from the parent the same way you are currently, except specify the parent instead of the current page when checking.

You can also put this snippet of code in your functions.php file and use it anywhere on your wp code.
function is_subpage() {
global $post; // load details about this page
if ( is_page() && $post->post_parent ) { // test to see if the page has a parent
$parentID = $post->post_parent; // the ID of the parent is this
return $parentID; // return the ID
} else { // there is no parent so...
return false; // ...the answer to the question is false
};
};
Source: http://codex.wordpress.org/Conditional_Tags

Related

Hide plugin from admin bar & plugin list

I am trying to hide the following item from the following sections:
Admin bar: ID = wp-admin-bar-nitropack-top-menu
Plugin list: data-slug="nitropack"
I have tried these methods, but can not get it to work. Maybe i have the wrong IDs/Slugs?
Methods: https://divi.space/wordpress-and-divi-code-snippets/hide-any-plugin-from-the-wordpress-dashboard/
Would really appreciate some help, since a customer should not be able to change the settings within this plugin!
Best regards,
Isac
The css way
<style>
a[data-slug="nitropack"] { //hides all a href's with that data slug
display:none;
}
</style>
normally if its an wp admin menu you would do something like this:
//remove admin page item
function edit_admin_menus() {
remove_menu_page("index.php"); //Dashboard
remove_menu_page("itsec"); // wp-admin.php?page=itsec use this "itsec"
}
add_action("admin_menu", "edit_admin_menus");
or you need to remove admin bar item
//remove tool bar item
function remove_toolbar_node($wp_admin_bar) {
// replace 'updraft_admin_node' with your node id "nitropack" something
$wp_admin_bar->remove_node("avia");
$wp_admin_bar->remove_node("updates");
$wp_admin_bar->remove_menu("wp-logo");
$wp_admin_bar->remove_menu("themes");
$wp_admin_bar->remove_menu("widgets");
$wp_admin_bar->remove_menu("dashboard");
//$wp_admin_bar->remove_node("updraft_admin_node");
}
add_action("admin_bar_menu", "remove_toolbar_node", 999);
FYI, since you need to block access to the plugin you'll need to add a redirect based on member role. The customer may know the actual url and can still access the page.
//Admin or Editor role check, if else send to alt url
function block_pages_for_user() {
$blocked_pages = is_page('slug');
$url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if( !current_user_can('administrator') && !current_user_can('editor') && !current_user_can('subscriber') && $blocked_pages ) {
wp_redirect( 'http://www.example.dev/your-page/', 301 );
exit;
}
}
add_action( 'wp', 'block_pages_for_user', 8 );

Selecting child pages with a specific parent page doesn't work

I need to add a specific function when child pages of a specific parent page are visualized.
My parent page has ID= 115 and it hase a 7 child pages. I used the following code
global $post; // load details about this page
if(is_page()&&($post->post_parent== '115)) {
echo ' This is a subpage';
}
else {
echo 'This is not a subpage';
}
Though it should work, the output echo, when I visualize one of the child pages, is "This is not a subpage" -> it means that it doesn't recognize it as a child page.
What is wrong? Thank you very much
Want to detect whether a specific Page on a WordPress has children or not?
This function comes in handy, just add it to your functions.php:
function has_subpage() {
global $post;
if($post->post_parent){
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
} else {
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
} if ($children) {
echo ' This is a subpage';
} else {
echo ' This is a not subpage';
}
}
You can achieve the desired result by taking the following steps listed below...
Within your child theme, on your page.php first check if your are on a sub page via $post->post_parent.
Then within that, you can return an array of ancestors via get_post_ancestors
Then setup a condition to get the ID of the top level parent page
Once you have the ID, you can easily target the parent page by name. I recommend this so that when your theme travels for your development environment to our live environment, or if you had to manually create pages in any of these environments, the names will be the same.
Finally, enter your code for the sub page that targets its specific parent page location.
if (is_page() && $post->post_parent) :
$parents = get_post_ancestors($post->ID);
$id = ($parents) ? $parents[count($parents) - 1] : $post->ID;
$parent = get_post($id);
$parent_slug = $parent->post_name;
if ($parent_slug == "slug-of-your-specific-parent-page") {
//TEST
echo "this is custom text displaying on my sub page for a specific parent page";
//OR ADD YOUR SPECIFIC FUNCTION
...
}
endif;

How do we get admin current screen by post id instead of screen id?

I need to add css on one single admin page which has post=183 .. Here id is 183.. The below code will work for all the pages but in my case I just want this to have effect in single page. So the best thing will have to be through page/post_id.
function this_screen() {
$current_screen = get_current_screen();
if(( $current_screen ->id === "page") ) {
wp_enqueue_style(
'gp_fp',
get_template_directory_uri() . '/admin/css/block.css');
}
}
Appreciate help.
I would check that:
Current page is admin (so it won't affect the front-end)
Current page is post ID = 183
$post_id = 183;
if (is_admin() && get_post_status($post_id) === false) {
// do something
}
Reference: https://tommcfarlin.com/wordpress-post-exists-by-id/

Wordpress shortcode - weird position on page

I created shortcode in my child themes functions.php file.
The shortcode is as follows:
function add_login_form() {
if ( is_user_logged_in() )
{
echo 'Witaj zalogowany';
}
else
{
wp_login_form();
}
}
add_shortcode('login_form', 'add_login_form');
I added shortcode [login_form] to my website (test area):
http://s540141209.domenaklienta.pl/wordpress/konto-klienta/
It is added to website after the text:
"TUTAJ POWINIEN BYĆ LOGIN FORM LUB NAPIS „WITAJ ZALOGOWANY”!"
It should be inside first div: <div class="et_pb_row">
But it is shown just after the header of a page. Have you any idea why it happens that way?
Thansk in advance.
But it happens to show before everything on website.
See http://codex.wordpress.org/Shortcode_API on how shortcodes acts. In a shortcode function you should always return the result of the processing done in the shortcode action. Echoing it outputs it as soon as the hook is executed, instead of being inserted in where it should be.
function add_login_form() {
if ( is_user_logged_in() )
{
return 'Witaj zalogowany';
}
else
{
return wp_login_form(array('echo'=>false));
}
}
add_shortcode('login_form', 'add_login_form');
For all arguments of wp_login_form, see http://codex.wordpress.org/Function_Reference/wp_login_form .

Manually highlight Wordpress admin menu item

Under Pages menu in Wordpress Admin page, I got this layout:
Pages
Edit (url: edit-pages.php)
Add New (url: page-new.php)
Special Pages (url: edit-pages.php?special-pages=true)
as you can see, I've added a new submenu item called Special Pages which is pretty much a link to to Edit page with custom filter. Because Wordpress use file name to identify and highlight the submenu item, so whenever I click on Special Pages, the Edit submenu item is selected. Is there anyway to force Wordpress to select Special Pages menu item instead?
Cheers
better solution:
add_filter('parent_file', 'my_plugin_select_submenu');
function my_plugin_select_submenu($file) {
global $plugin_page;
if ('__my-current-submenu-slug__' == $plugin_page) {
$plugin_page = '__my-submenu-slug-to-select__';
}
return $file;
}
To further clarify Ken Vu's answer, edit the global variables $submenu_file and $parent_file. E.g., to highlight your page:
global $submenu_file;
$submenu_file = "edit-pages.php?special-pages=true";
If you need to change the top-level item highlighted, use $parent_file. E.g., highlight the "Writing" setting page:
global $parent_file;
global $submenu_file;
$parent_file = 'options-general.php';
$submenu_file = 'options-writing.php';
Solution: use $submenu_file variable
$submenu_file = "edit-pages.php?special-pages=true"
Thanks Ken Vu and Jonathan Brinley. Using your answers, I finally got the highlighting of my admin menu to work properly. As I struggled a bit to get it to work, I though I would post the entire result here, so other people can find it more easily :
The idea is to call the parent_file filter (undocumented, as many Wordpress parts unfornatunately). In my case, I was adding a custom menu instead of the default generated when creating a custom post type.
In my custom post code, I call the add_meta_boxes action. Within this hook, I issue my call to the parent_file filter :
add_filter('parent_file', array(&$this, 'highlight_admin_menu'));
_
Then this is how my hightlight_admin_menu function looks like :
function highlight_admin_menu($some_slug){
global $parent_file;
$parent_file = 'post.php?post=149&action=edit';
return $parent_file;
}
_
This got my menu to highlight properly. Try playing around with you own code to know where to issue the add_filter('parent_file', ...) code. Find a bit of code executed only on that particular page load, but soon enough that it is still possible to modify the $parent_file variable.
I hope this helps!
For changing the highlighted menu item for a submenu item, the proper filter is submenu_file.
add_filter('submenu_file', 'menuBold');
static function menuBold($submenu_file)
{
if ( checkProperPage($_GET) ) {
// The address of the link to be highlighted
return 'post-new?post_type=foobar&foo=bar';
}
// Don't change anything
return $submenu_file;
}
The check happens in WP's ~/wp-admin/menu-header.php file on line 194 (Wordpress 4.5.3):
if ( isset( $submenu_file ) ) {
if ( $submenu_file == $sub_item[2] )
$class[] = 'current';
...
}
You can modify this code to work for you. You can change both parent and submenu with that. Tested code.
function change_active_parent($submenu_file)
{
global $parent_file;
$zone = 'edit-tags.php?taxonomy=zone&post_type=product';
$storefront = 'edit-tags.php?taxonomy=storefront&post_type=product';
$container = 'edit-tags.php?taxonomy=container&post_type=product';
if (esc_html($zone) == $submenu_file) {
$parent_file = 'parent';
$submenu_file = $zone;
}
elseif (esc_html($storefront) == $submenu_file) {
$parent_file = 'parent';
$submenu_file = $storefront;
}
elseif (esc_html($container) == $submenu_file) {
$parent_file = 'parent';
$submenu_file = $container;
}
return $submenu_file;
}
add_filter( 'submenu_file', 'change_active_parent' );
Use the load-{$page_hook} action hook and modify the necessary globals:
/**
* For giggles, lets make an admin page that is not "in the menu" to play with.
*/
add_action('admin_menu', 'mort1305_admin_menu');
function mort1305_admin_menu() {
add_submenu_page(
NULL,
'Page Title',
'',
'administrator',
'my_slug',
'mort1305_page_content'
);
}
/**
* The menu item to highlight and the submenu item to embolden.
*/
add_action('load-admin_page_my_slug', 'mort1305_on_page_load');
function mort1305_on_page_load(){
global $plugin_file, $submenu_file, $title;
$plugin_page = 'slug-of-menu-item-to-be-highlighted';
$submenu_file = 'slug-of-submenu-item-to-be-bold';
foreach($submenu[NULL] as $submenu_arr) {
if($submenu_arr[2] === 'test_page_slug') {
$title = $submenu_arr[3];
break;
}
}
}
/**
* Page content to display.
*/
function mort_1305_page_content() {
echo This is the '. get_admin_page_title() .' page. The slug of my parent is '. get_admin_page_parent() .'.';
}

Resources