WordPress Admin Area - Add class to body of page edit screen - php

I used wp-types toolset to create a custom post type and a post relationship to pages; there is now a Post Relationships section at the bottom of every page edit screen. The problem is, I would only like this section to show up on a couple of pages.
Is there something I can add to functions.php (or another alternative) to hide this section from all page edit screens expect for those particular ones.
The section div id that I want to hide is #wpcf-post-relationship and the data post id of the pages that I would like it to be visible are 143 and 23.

-- (update) --
As admin_init is triggered before any other hook when a user access
the admin area, we finally use instead admin_head because action is
just triggered inside the <head> of the admin page (thanks to John).
The easy way is to use a simple CSS rule with the 'admin_head' hook, to do it, like this:
1) create a css file named hide_some_field.css and put it into your active child theme folder, with this code:
#wpcf-post-relationship {
display:none;
}
2) Add this code in your active child theme functions.php file:
add_action('admin_head', 'ts_hiding_some_fields');
function ts_hiding_some_fields(){
// your 2 pages in this array
$arr = array(23, 143);
if(get_post_type() == 'page' && !in_array(get_the_ID(), $arr))
{
wp_enqueue_style( 'hide_some_field', get_stylesheet_directory_uri().'/hide_some_field.css');
}
}
If you use a theme instead, change:
get_stylesheet_directory_uri() by get_template_directory_uri().
Another similar alternative (without an external CSS file) is:
add_action('admin_head', 'ts_hiding_some_fields');
function ts_hiding_some_fields(){
// your 2 pages in this array
$arr = array(23, 143);
if(get_post_type() == 'page' && !in_array(get_the_ID(), $arr))
{
echo '<style type="text/css">
#wpcf-post-relationship {display: none;}
</style>';
}
}

Related

How can I make a button in wordpress theme redirect to a specific template onclick?

So I have this code in php:
add_filter( 'template_include', 'my_callback' );
function my_callback( $original_template ) {
if ( some_condition() ) {
return SOME_PATH . '/some-custom-file.php';
} else {
return $original_template;
}
}
and I want to re-write this code in order to use it inside an <a> element and specifically in an onclick="" attribute of that <a> element. All in all my main goal is to create 2 template files in my theme's folder where one is for most recent posts and the other is for the most popupal posts, and the use the code above but inside the <a> element to be able to redirect to each one respectively.
Any suggestions? Thanks in advance,
Jameu
Create two wordpress native pages (inside WP Backend), use the template you want (create it) with each one.
Here is the template pages doc:
TEMPLATE FILES
Once you have done it just link them with regular html: LINK PAGE1 or with javascript if you want

Change link of Wordpress menu item for posts

I have a menu that is used throughout the entire site (pages and posts). I would like to change the link of one single menu item (custom link) if the menu is displayed on on a blog post.
I tried using the plugin Conditional Menus which could do the job, but has the disadvantage that I would need to recreate the entire menu for each language and repeat this everytime the menu is updated.
I think it would be smarter to do this through php. I found this to check post vs. page:
<?php
if(get_post_type() === 'post') {
// Do something
}
?>
Would it be possible to set the href of the <a> tag in menu-item-123 within this if-statement? Also, is there a way I can add this to functions.php in the child theme or does it need to placed somewhere else? Thanks!
Please write following codes in your functions.php file Here ".primary-menu" WILL BE the css class of your menu UL, I've setup 3rd child in the menu item, you can change it accordingly.
<?php
add_action( 'wp_footer', 'change_link' );
function change_link(){
if(get_post_type() === 'post') { ?>
<script>
(function($) {
jQuery('.primary-menu li:nth-child(3) a').attr("href", "https://google.com");
})(jQuery);
</script>
<?php }
}

Edit post pages with PHP and CSS in Wordpress?

I would edit the pages of posts (eg: http://example.com/author/wordpress_post) with PHP and CSS. How can I do it? Is there already a default PHP file for posts that I can modify?
You may need to read up on WordPress' Template Hierarchy
Posts, by default, use the single.php file, located in the theme directory. Note if you have a Parent Theme and an active Child Theme, your child theme may not have a single.php file. You can, however, copy the single.php file from the parent to the child and modify it from there, knowing that if the parent theme structure changes, your child theme may need to accommodate.
If your theme uses a different structure, you can always modify the single post functionality with the is_single() function in your functions.php file. If you just want to edit post titles, you could put this code in functions.php
function my_title_filter( $title, $post_id = null ) {
if( is_single() ){
// Only modifies Post titles, no other titles.
$title = 'Add This Before Post Titles '. $title;
}
return $title;
}
add_filter( 'the_title', 'my_title_filter' );
For CSS, just, well, use CSS. Open up style.css or the customizer and go to "Additional CSS" and make the changes you want. On single posts, the body (should) have the class single-post so you can filter out CSS changes only for posts by prefixing your selectors with that.
/* Make paragraphs a light blue, but only on single posts */
.single-post p {
color: #0095EE;
}

Is it possible to insert shortcode inside Header menu or top-bar menu in wordpress?

I have made a simple shortcode for my wordpress theme. I'm trying to align it on right-side above the corner of my page. Is it possible to insert shortcode function.php?
I want to place my Short-code in the top-bar (right side above the corner) so that it will work for each pages. Means if i click on any menu then the shortcode should have to work on that menu also.
Actually i'm new in web development. Please suggest me what shall i have to do so it will align where i want and should have work for each module.
You can use wordpress function for that. Please open the file on which you have to place shortcode. Like if you want to place it on header open header.php and add
<?php echo do_shortcode('[your_shortcode]'); ?>
where you want your shortcode content to appear
The best way is to find the top bar menu hook of your theme and then add this code (adapted for your needs) in your function.php child theme :
function my_shotcode_inside_top_bar ( $items, $args ) {
if (is_page() && $args->theme_location == 'secondary') {
$items .= '<li class="my-class">' . do_shortcode( "[my_shortcode]" ) . '</li>';
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'my_shotcode_inside_top_bar', 10, 2 );
is_page displays the shortcode in pages only but you can choose another condition or no condition at all.
secondary is the hook and it depends on your theme. You can normally find it in your theme header.php file.
A menu location selector should be added - that way some themes support multiple menus, and some are called "top" or custom added.
The shortcode could then contain a call to whatever a menu position is:
EG: [myshortcode menu="secondary"]
Didn't have enough points to add this as a comment.

How to hide plugins style sheets in wordpress

i want to hide few plugins style sheets to reduce load on our Index page and categories pages. Actually we want to display plugin style sheet only on Post not on other pages.
we have used following code in plugin, but it doesn't work. please help how to use it.
if( is_single() || is_singular('post') ) wp_enqueue_style('savrix-style.css');
If you are modifying your own plugin I see no reason your code wouldn't work. The is_single() condition is not needed, and will result in the stylesheet being loaded on custom post types and other singles that you don't intend.
However your wp_enqueue_style call is incomplete, so unless you have a wp_register_style call somewhere else defining the handle and URL of the stylesheet you need to change it to something along these lines:
if (is_singular('post')) {
wp_enqueue_style('savrix-style', plugins_url('savrix-style.css', __FILE__);
}
However, I get the impression that you are actually trying to remove a stylesheet included by a thirdparty plugin. It is generally a bad idea to modify a third-party plugin, as your modifications will be lost on the next update... it is very difficult to maintain that sort of modifications in the long run.
Instead make a new plugin and modify whatever you need from there.
What you want to achieve can be accomplished by:
Create a new folder in the wp-content/plugins folder, fx. my_load_reducer.
Inside that folder create a new file called my_load_reducer.php
Paste this in the file:
<?php
/*
Plugin Name: My Load Reducer
Description: Removes unneeded and unwanted stylesheets from other plugins
Version: 0.1
*/
//Use a class to avoid conflicts
class my_load_reducer {
function __construct() {
//Hook into wp_enqueue_scripts with a high priority
add_action( 'wp_enqueue_scripts', array($this, 'deregister_styles'), 1000 );
}
function deregister_styles() {
//Check that current post is not a single post
if (!is_singular('post')) {
//deregister the stylesheet - this removes the twentyfifteen
//main stylesheet - obviously you need to substitute the handle
//of the stylesheet you actually want to remove
wp_deregister_style( 'twentyfifteen-style' );
}
}
}
//Instantiate the class
$my_load_reducer = new my_load_reducer();
Activate the plugin through the wordpress admin.
You can remove perticular plugin css on selected page.
below code is remove plugin css to other pages and display only on post pages:
/*disable loading plugin css to page and load on post page*/
add_action('wp_print_styles', 'my_deregister_styles', 99999);
function my_deregister_styles()
{
if(!is_single())
{
wp_dequeue_style('plugin-css-handle');
wp_deregister_style('plugin-css-handle');
}
}
where 'plugin-css-handle' is perticular plugin's css handle which you want to remove.

Categories