How do you remove or change the functionality of the Publish button on a custom WordPress post? - wordpress

I have a custom post type and need keep the post status from getting set to 'Published' when you click the Publish button. Instead, it should work like the Save Draft button. So I either need to figure out how to just remove the Publish button so the user's can only click Save Draft our preferably, update the Publish button functionality so it doesn't set the post to publish.

You can use wordpress action hooks to modify default behaviors.
http://codex.wordpress.org/Function_Reference/add_action
In your case, you want to use the 'publish_post' hook.
So you can do
function dont_publish( $post_ID )
{
if(get_post_type($post_ID) == 'your_custom_type'){
exit;
}
}
//the dont_publish function will be called after the publish button is clicked
add_action( 'publish_post', 'dont_publish' );
The way it is above, nothing will happen at all if the publish button is clicked, but you can play around with the dont_publish function to get the results you want.

#PhoenixWing156 was close but one little change so the the other post types get updated as usual.
function dont_publish( $data , $postarr ) {
if($data['post_type'] == 'custom_post_type') {
$data['post_status'] = 'draft';
}
return $data;
}
add_filter('wp_insert_post_data' , 'dont_publish' , '99', 2);

The wp_insert_post_data hook is called before information about a post is saved to the database.
http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
You can try:
function dont_publish( $data , $postarr )
{
if($data['post_type'] == 'custom_post_type'){
$data['post_status'] = 'draft';
return $data;
}
}
add_filter('wp_insert_post_data' , 'dont_publish' , '99', 2);

WordPress provides the remove_meta_box() function exactly for this purpose.Just add this below code:-
add_action( 'admin_menu', function () {
remove_meta_box( 'submitdiv', 'Your_custom_post_type', 'side' );
} );

You could also disable the default saving metabox and add you own.
This is not documented well in the developer docs of wordpress.
To do this you have to hook into the "add_meta_boxes"-hook and in the hooked function yo have to call remove_meta_box('submitdiv','your-cpt','side');
The code should be something like this:
function your_cpt_metaboxes(){
remove_meta_box('submitdiv','your-cpt','side');
...
}
add_action('add_meta_boxes','function your_cpt_metaboxes');
your-cpt has to be changed to the name of your cpt of course.
I was also searching for this handy snippet and found it in the plugin Awesome Support.
The original saving metabox code can be found in /wp-admin/includes/metaboxes.php .
Just search for post_submit_meta_box (in WP 5.4 on line 22).

Related

How to hide some Custom Fields from Woocommerce Admin order page

When logged in as an admin and looking at an Order in Woocommerce, there's a section with all the Custom Fields. Out of the whole list I only want it to display two of them. How do I hide the rest from this view? I don't want to delete them, but just hide from this view.
For every custom field you want hidden, add the following 4 lines of code to functions.php or using Snippets plugin:
add_filter('is_protected_meta', 'my_is_protected_meta_filter1', 10, 2);
function my_is_protected_meta_filter1($protected, $meta_key) {
return $meta_key == 'automatewoo_cart_id' ? true : $protected;
}
If you want to hide more than one, add the lines above again and change 'my_is_protected_meta_filter1' to 'my_is_protected_meta_filter2', etc
if you’re using ACF pro, there is a hook you can use to remove the field on the back end, but it’s not something that’s documented..
You could use a hook to remove specific field if is_admin() returns true.
You may need to play with this a bit to get it to work, the ACF hook is
acf/get_fields
So, for example:
add_filter('acf/get_fields', 'your_function_name', 20, 2);
function your_function_name($fields, $parent) {
// remove the fields you don't want
return $fields;
}
$fields can be a nested array of fields => sub_fields.
You need to set the priority > 10 to run after the internal ACF filter
For orders in Woocommerce the post type is 'shop_order', so your code should be:
add_action( 'add_meta_boxes', 'remove_shop_order_meta_boxe', 90 );
function remove_shop_order_meta_boxe() {
remove_meta_box( 'postcustom', 'shop_order', 'normal' );
}

How to set a WooCommerce email template as default for all emails

I’m looking for a way to send all WordPress emails using a custom WooCommerce template so all emails will look the same.
The path to the template would be:
woocommerce/emails/my-custom-woocommerce-template.php
Does it have to all be templatized in a single file? If not, a combination of these entry points can probably get you the standardization you're looking for:
email-header.php lets you customize the start of the email including the header image (if you need to do more than change its URL). It opens the layout tags for the rest of the email content
email-footer.php lets you customize the footer, and closes the layout tags started in the header.
email-styles.php or the woocommerce_email_styles filter let you customize the CSS (see some gotchas in my article here).
Various actions/filters are scattered throughout the emails for customizing individual parts.
You can use the below function. It is working
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
// List of all templates that should be replaced with custom template
$woo_templates = array(
'emails/admin-new-order.php',
'emails/admin-failed-order.php',
'emails/admin-cancelled-order.php',
'emails/customer-completed-order.php',
'emails/customer-new-account.php',
'emails/customer-note.php',
'emails/customer-on-hold-order.php',
'emails/customer-processing-order.php',
'emails/customer-refunded-order.php',
'emails/customer-reset-password.php',
);
//Check whether template is in replacable template array
if( in_array( $template_name, $woo_templates ) ){
// Set your custom template path
$template = your_template_path.'emails/my-custom-woocommerce-template';
}
// Return what we found
return $template;
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
add_filter( 'wp_mail', 'your_wp_mail_action' ); // $args = compact( 'to', 'subject', 'message', 'headers', 'attachments' )
function your_wp_mail_action( $args ) {
global $your_prefix_your_email_args; // the args you could use in my-custom-woocommerce-template file
$your_prefix_your_email_args = $args;
ob_clean();
get_template_part( 'woocommerce/emails/my-custom-woocommerce-template' );
$args['message'] = ob_get_clean();
// ... your logic
return $args;
}
To view and update email settings, log into your website dashboard. In the left-hand menu, click on WooCommerce → Settings.
There, you’ll find several options tabs at the top. Click Emails to view the following templates
you can custom all as you want

WooCommerce Registration Shortcode - Error messages problems

I am currently creating a widget to display the registration form on a WordPress website that uses WooCommerce. For now, I only have 3 basic fields which are email, password, repeat password. I'm looking forward to add more WooCommerce fields, but want to solve that problem before jumping to the next step.
I'm having some problems with the messages output (wrong password, account already exists, etc).
I searched on the web and there was no shortcode already built for WooCommerce registration, beside their registration page. So I went ahead and created a shortcode, with a template part.
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
This is a snippet I use to get the template part inside a variable, since the default function would "echo" the content instead of "returning" it.
function load_template_part($template_name, $part_name=null) {
ob_start();
get_template_part($template_name, $part_name);
$var = ob_get_contents();
ob_end_clean();
return $var;
}
So, the problem is, when I call woocommerce_show_messages or $woocommerce->show_messages(); from my template part, nothing is showing, or if it is, it shows at the top of the page.
I did try to put the calls inside my shortcode function:
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$woocommerce->show_messages();
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
Doing so, the message output inside the <head> tag, which is not what I want.
I tried to do the same trick with ob_start(), ob_get_contents() and ob_clean() but nothing would show. The variable would be empty.
I also did try to hook the woocommerce_show_messages to an action as saw in the core:
add_action( 'woocommerce_before_shop_loop', 'woocommerce_show_messages', 10 );
For something like:
add_action( 'before_registration_form', 'woocommerce_show_messages');
And I added this in my template-part:
<?php do_action('before_registration_form'); ?>
But I still can't manage to get the error messages show inside the box. It would always be inserted in the <head>
I will share final solution when everything is done.
Thanks for your time,
Julien
I finally got this working by hooking a custom function to an action which is called in my header.php
I guess hooking functions inside template part does not work as intended.
In header.php, I got this:
do_action('theme_after_header');
And here's the hooked function. Works perfectly.
function theme_show_messages(){
woocommerce_show_messages();
}
add_action('theme_after_header', 'theme_show_messages');
However, I will look into 'unhooking' the original show message function since it might show twice. Need to test some more ;)
You can also just use the [woocommerce_messages] shortcode in your template where you want it displayed
Replying to a bit of an old question, but you can also try the following:
$message = apply_filters( 'woocommerce_my_account_message', '' );
if ( ! empty( $message ) ) {
wc_add_notice( $message );
}

Wordpress Plugin: Show html only on standard page and not in admin area

I'm writing a plugin and I need to display a piece of text in the WP page, but not in the admin area. How can I do so?
I tried this in the construct:
add_action( 'init', array( $this, 'initPage' ) )
and then:
public function initPage() {
echo 'hello';
}
but the text is displayed also in the admin area. Is there a way to do this? It would be the opposite of the action admin_init I assume.
Proper way to handle it: is_admin()
http://codex.wordpress.org/Function_Reference/is_admin
if(is_admin()) { // do nothing } else {
// function you want to execute.
}
I solved this by adding it to a shortcode action. Like this:
add_shortcode( 'myPlugin', array( $this, 'shortcode' ) );
and:
public function shortcode( $atts ) {
return 'hello';
}
With the above code, 'hello' will only display on the front-end. Not sure if that's the cleaner way to do it, but does the job.
There is no "front-end-only" version of init, however you probably don't want to be doing any output at the init action anyway.
What exactly are you trying to do? Usually, you use an action hook for specific types of things, and causing output very early at something like "init" is rare and weird.

Running a function in Wordpress when publishing a custom post type

I am trying to run the following function anytime a wordpress "Jobs" custom post is published. The code works (lines 2-7) when it is placed in my theme template, but it only runs when the post is viewed. I want the code to run when the post is published, and so I have tried adding the code in a function inside my functions.php, but nothing is happening when each custom post is published. Any suggestions?
function indeedgeo(){
$indeedgeo = get_post_meta($post>ID, indeedgeo, true);
$indeedgeos=explode(' ',$indeedgeo);
$_jr_geo_latitude = $indeedgeos[0];
$_jr_geo_longitude = $indeedgeos[1];
update_post_meta($post->ID, _jr_geo_latitude, $_jr_geo_latitude);
update_post_meta($post->ID, _jr_geo_longitude, $_jr_geo_longitude);
}
add_action('publish_Jobs', 'indeedgeo');
You should hook into one of the three actions;
do_action('edit_post', $post_id, $post);
do_action('save_post', $post_id, $post);
do_action('wp_insert_post', $post_id, $post);
that are run when a post is either saved, or has its status updated. Something like the following should do the trick.
function se_10441543_save_post($post_id, $post){
//determine post type
if(get_post_type( $post_id ) == 'your_post_type'){
//run your code
$indeedgeo = get_post_meta($post_id, indeedgeo, true);
$indeedgeos=explode(' ',$indeedgeo);
$_jr_geo_latitude = $indeedgeos[0];
$_jr_geo_longitude = $indeedgeos[1];
update_post_meta($post_id, _jr_geo_latitude, $_jr_geo_latitude);
update_post_meta($post_id, _jr_geo_longitude, $_jr_geo_longitude);
}
}
add_action('save_post', 'se_10441543_save_post', 10, 2);
http://codex.wordpress.org/Plugin_API/Action_Reference
Not exactly sure how your "publish_jobs" hook is working but right off the bat, if you are placing this function in your functions.php, you will need to give it context. Replace $post>ID with the post number (an integer). If this applies to many post you will probably want to use another method of querying post data: http://codex.wordpress.org/Class_Reference/WP_Query. Let me know if this helps.

Resources