How to customize publish area in WordPress? - wordpress

I want to add a text message in "publish" area in admin panel.
Are there any filters or actions to edit?
http://codex.wordpress.org/Plugin_API/Action_Reference
such as
add_atction('in_admin_footer', 'blahblah');
function blahblah($content) {
$content .= '<strong> Please make sure you input correct data</strong>';
return $content;
}
The 'in_admin_footer' action helps to add a message to the real footer of admin panel.
I want to Publish area.
http://gyazo.com/8342ffb94ae1a829988257bf2506c3d6
(string(0) "" is addded by in_admin_footer action)

Try the post_submitbox_misc_actions action hook. Something along these lines:
function submitbox_callback() {
global $post;
if ($post->post_type == 'post') { //if you only want to display this on posts
echo '<strong> Please make sure you input correct data</strong>';
}
}
add_action( 'post_submitbox_misc_actions', 'submitbox_callback' );

Related

Where exactly is the "Before Header Content hook" in Wordpress?

I want to add a little code to the "Before Header Content hook" but I don't know where that is... Can you please help me?
Try:
add_action('init', 'process_post');
function process_post()
{
echo "test";
}
this is a modified version of #ajay 's solution
if you are going to use this, then you have to make sure that the current user is not an admin using is_admin() function.. and only display it if he is not an admin ...
why !?
because if you didn't, it may mess up the wp-admin of your website..
add_action('init', 'process_post');
function process_post()
{
if (!is_admin()) {
echo "test";
}
}
This could be tricky simply because every theme differs with how the loop is displayed, however you could create a plugin to use the loop_start action, which is called before the first post of the standard WP loop:
add_action( 'loop_start', 'test_loop_start' );
function test_loop_start( $query ){
echo 'this is my inserted text';
}
Now using this would display it every single time the loop is called (whether on a page, a post, category page, search page, etc.), which you may not want.
add_action( 'loop_start', 'test_loop_start' );
function test_loop_start( $query ){
if(is_category() OR is_singular()) {
echo 'this is my inserted text';
}
}

How to hide advanced custom fields(ACF) in the WP-admin UI?

Check the screenshot below; all I want to do is to hide certain ACF fields for custom users in the wordpress backend.
As of ACF 5.0.0 there is an easier way to do this without having to output CSS. If you use the acf/prepare_field hook and return false the field will not render.
<?php
function so37111468_hide_field( $field ) {
// hide the field if the current user is not able to save options within the admin
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
return $field;
}
add_filter( 'acf/prepare_field/key=MYFIELDKEY', 'so37111468_hide_field' );
?>
The documentation for that filter can be found here: https://www.advancedcustomfields.com/resources/acf-prepare_field/
If you mean to hide it with CSS, then you should insert custom CSS to admin footer area.
For example, you can add such kind of code to your theme's functions.php file:
add_action('admin_footer', 'my_admin_hide_cf');
function my_admin_hide_cf() {
$u=wp_get_current_user();
$user_roles = $u->roles;
if ($user_roles[0]=='CUSTOM_USER_ROLE_NAME'){
echo '
<style>
#acf-FIELD_SLUG_HERE {display:none}
</style>';
}
}
And of course you should replace FIELD_SLUG_HERE and CUSTOM_USER_ROLE_NAME values with correct ones.
F.e. #acf-FIELD_SLUG_HERE can be #acf-url, CUSTOM_USER_ROLE_NAME can be "contributor".

Remove WordPress admin bar on a single page

Is there a way to remove the WordPress admin bar but just on a specific page?
I know that I can remove it or hide it completely by adding an action:
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
I've done that but on my site I have one page that is displayed in an iframe (using colorbox) and I don't ever want the admin bar to show up in that iframe.
Is there a way to hide the admin bar but just for a specific page?
Thanks,
Ben
Your function is a bit complicated, there's a filter for that. Just check for the Page ID and filter it out using the show_admin_bar filter.
function riga_hide_admin_bar(){
if( $post->ID == YOUR_POST_ID ){
return false;
}
}
add_filter( 'show_admin_bar' , 'riga_hide_admin_bar' );
Remove WordPress admin bar on a single page
Yes, you can remove admin bar from a specific page as you wish.
Use add_filter( 'show_admin_bar', '__return_false' ); to hide admin bar from a specific page.
function my_function_admin_bar(){
return false;
}
add_filter( 'show_admin_bar' , 'my_function_admin_bar');
For more details please see below link:
https://codex.wordpress.org/Plugin_API/Filter_Reference/show_admin_bar
Get ID of specific page and add your filter in condition which meet the page ID
`If(get_the_ID()==#736637){
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
}`
For me the easiest way is to add the code in the template of the page I don't want to show. At the top of the template file I just add:
<?php
show_admin_bar(false);

How to add a button to a custom post type in Wordpress?

I have a "Products" custom post type. Normally, this custom post type have an "Add New" button. I want to add another button call "Update from Provider".
Currently, I have modify the Wordpress code (in "wordpress\wp-admin\includes\class-wp-list-table.php") to add that button. In this case, when I update Wordpress, my modified code will be deleted. Therefore, I need to move that button to my plug-in code.
In this case, please help me how to move that button to my plug-in code.
Well, if you opened the core file you saw that there's no action in it where we can hook.
Only a couple of filters. We can use the following:
add_filter( 'views_edit-movies', 'so_13813805_add_button_to_views' );
function so_13813805_add_button_to_views( $views )
{
$views['my-button'] = '<button id="update-from-provider" type="button" title="Update from Provider" style="margin:5px">Update from Provider</button>';
return $views;
}
It produces this:
To put it in an approximate position from where you'd like, use the following:
add_action( 'admin_head-edit.php', 'so_13813805_move_custom_button' );
function so_13813805_move_custom_button( )
{
global $current_screen;
// Not our post type, exit earlier
if( 'movies' != $current_screen->post_type )
return;
?>
<script type="text/javascript">
jQuery(document).ready( function($)
{
$('#update-from-provider').prependTo('span.displaying-num');
});
</script>
<?php
}
Which results in this:

Redirect from add_menu_page

I have added a menu using add_menu_page which all works correctly, but when clicked I want this menu page to open the post editor for a particular post id.
As a proof of concept i have tried echoing a javascript redirect out into the do function like so...
// Load up the menu page
function register_availability_custom_menu_page() {
add_menu_page('custom menu title', 'Availability', 'add_users', 'options', 'options_do_page');
}
function options_do_page() {
echo "<script type=\"text/javascript\">";
echo "window.location = '/whatever_page.php';";
echo "</script>";
}
This approach does work but I was wondering if it is the best approach, is there a better way to redirect to the page I am after?
UPDATE
I have now also tried using wp_redirect with this code...
add_action( 'admin_menu' , 'admin_menu_new_items' );
function admin_menu_new_items() {
wp_redirect( home_url() );
exit;
}
This gives me a Headers already sent error, can anyone suggest where I am going wrong?
If I'm understanding this correctly, you don't need the redirect. Instead of using a $menu_slug in the function add_menu_page, put the address of the target page, e.g.:
$the_post_title = 'The Portfolio';
add_action( 'admin_menu', 'wpse_59050_add_menu' );
function wpse_59050_add_menu()
{
global $the_post_title;
$our_page = get_page_by_title( $the_post_title );
$settings_page = add_menu_page(
'Edit '.$our_page->post_title,
'Edit '.$our_page->post_title,
'add_users',
'/post.php?post='.$our_page->ID.'&action=edit',
'',
'',
2
);
}
This function is from the following WordPress Answer: Highlighting a Menu Item by Post Name. You'll need some jQuery to do the correct highlighting of this top level menu, check both my and TheDeadMedic answers in that Q.
This other one is useful too: Add highlighting to new Admin Dashboard Menu Item.
Recently did this for pootle page builder, and this AFAIK is the best way,
/**
* Redirecting from Page Builder > Add New page
* #since 0.1.0
*/
function add_new() {
global $pagenow;
if ( 'admin.php' == $pagenow && 'page_builder_add' == filter_input( INPUT_GET, 'page' ) ) {
header( 'Location: ' . admin_url( 'whatever-page.php' ) );
die();
}
}
add_action( 'admin_init', 'add_new' );
Replace page_builder_add with your page slug and admin_url( 'whatever-page.php' ) with url you wanna redirect to.
We always use scrutinizer-ci for best code practices and maintain code score greater than 9.5/10, so all code (Including this) is secure and optimized ;)
Lemme know how it works for ya.
Cheers
I found a 2-step solution that doesn't use JS/JQ.
Step 1:
Near the top of your script, put the following PHP to display CSS that hides your first page:
add_action('admin_head', 'hide_my_first_page');
function hide_my_first_page(){
echo '<style>
a[href="admin.php?page=admin_menu_slug"] + ul > li.wp-first-item{
display: none;
}
</style>';
}
Where admin_menu_slug is the 4th argument passed to add_menu_page();.
Step 2:
Take the function of the page you want to run and pass it as the 5th argument in add_menu_page();

Resources