wordpress dashboard display custom error message upon custom condition - wordpress

In edit-user.php of wordpress dashboard before updating meta information I am checking one condition and upon failure of that condition I want to display a error message. I tried to echo div with updated class and also tried WP admin_notices hook but no luck
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
function save_extra_profile_fields( $user_id ) {
global $wpdb;
if(CONDITION TRUE) {
update_usermeta( ........... );
}
else {
WANT TO DISPLAY ERROR MESSAGE
}
}

There is a hook for validating user extra fields. This hook will call before updating user details.
You can display error message like this:-
add_action( 'user_profile_update_errors', 'validate_extra' );
function validate_extra(&$errors, $update = null, &$user = null)
{
if (!$_POST['YOUR_FIELD'])
{
$errors->add('YOUR_FIELD', "<strong>ERROR</strong>: YOUR ERROR MESSAGE.");
}
}
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
function save_extra_profile_fields( $user_id )
{
global $wpdb;
update_usermeta( ........... );
}

Related

Custom cron job doesn't scheduled in Wordpress

I read several questions here and from Google search results, but I wasn't able to make it work. I have a function I want to run once every day which will fetch data from external source and store it on options page.
add_action( 'wp', 'update_stock_market' );
add_action( 'get_stock_market_daily_data', 'get_stock_market_data', 1, 1 );
function update_stock_market() {
if ( ! wp_next_scheduled( 'get_stock_market_daily_data' ) ) {
wp_schedule_event( time(), 'daily', 'get_stock_market_daily_data', array($company) );
}
}
function get_stock_market_data($company ) {
//do something here to get $data;
update_option( 'stock_market_data', $data);
}
However, no matter what happens, I cannot find the scheduled cron in the wp-options table under the option-name of cron
This code is working for me on a clean instance of WordPress, instead of looking directly in the database have you tried using the plugin WP Crontrol? Alternatively, with debug enabled, try adding a log entry to you update_stock_market function to check that the sechudled event is being called.
add_action( 'wp', 'update_stock_market' );
add_action( 'get_stock_market_daily_data', 'get_stock_market_data', 1, 1 );
function update_stock_market() {
error_log( 'triggered' );
if ( ! wp_next_scheduled( 'get_stock_market_daily_data' ) ) {
wp_schedule_event( time(), 'daily', 'get_stock_market_daily_data', array($company) );
}
}
function get_stock_market_data($company ) {
//do something here to get $data;
update_option( 'stock_market_data', $data);
}

How to save Meta Data in WordPress back-end with PHP

I am relatively new to Post Meta Data in the WordPress backend using PHP. I have written the code that creates the Meta Data. I need help saving the data for which I have written. It will also need to allow me to edit the data once saved.
In this case its for a text field.
I have created the Meta Data for the input field which displays well in the back-end WordPress admin area.
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'Job Title', 'cd_meta_box_cb', 'people', 'normal', 'high' );
/* Save post meta on the 'save_post' hook. */
add_action( 'save_post', 'cd_meta_box_add', 10, 2 );
}
function cd_meta_box_cb()
{
echo "<input type='text' name='jobtitle'>";
}
I just need assistance with the code that will save the above Meta Data to the DB and allow for editing and revisions
You were almost there.
The final part of the puzzle is the function that saves the metadata, but first we need to make a few adjustments to your existing code:
add_action( 'save_post', 'cd_meta_box_add', 10, 2 ); has to be moved outside cd_meta_box_add(), and
Change add_action( 'save_post', 'cd_meta_box_add', 10, 2 ); into add_action( 'save_post', 'cd_meta_box_add' ); as this action hook only receives one parameter (the post ID), and
You need to define the function that will process the data (and it can't be cd_meta_box_add as you have it now so we'll create a new one called save_cd_meta_box_data).
/* Save post meta on the 'save_post' hook. */
add_action( 'save_post', 'save_cd_meta_box_data' );
function save_cd_meta_box_data( $post_id ) {
// Autosaving, bail.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// #TODO
// You should add some additional security checks here
// eg. nonce, user capabilities, etc, to prevent
// malicious users from doing bad stuff.
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['jobtitle'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['jobtitle'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_job_title', $my_data );
}
Now that we're successfully saving the metadata into the database, let's allow the user to view it / edit it:
function cd_meta_box_cb( $post )
{
$job_title = get_post_meta( $post->ID, '_job_title', true );
echo "<input type='text' name='jobtitle' value='" . esc_attr( $job_title ) . "'>";
}
The final code should look like this:
/* Register and display metabox */
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'Job Title', 'cd_meta_box_cb', 'people', 'normal', 'high' );
}
function cd_meta_box_cb( $post )
{
$job_title = get_post_meta( $post->ID, '_job_title', true );
echo "<input type='text' name='jobtitle' value='" . esc_attr( $job_title ) . "'>";
}
/* Save post meta on the 'save_post' hook. */
add_action( 'save_post', 'save_cd_meta_box_data' );
function save_cd_meta_box_data( $post_id ) {
// Autosaving, bail.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// #TODO
// You should add some additional security checks here
// eg. nonce, user capabilities, etc, to prevent
// malicious users from doing bad stuff.
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['jobtitle'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['jobtitle'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_job_title', $my_data );
}

Woocommerce - Remove moderate_comments Capability from Shop Manager

I want to remove the moderate_comments capability from the Shop Manager Role.
I am using the below code, which is not working at all.
add_action( 'init', 'add_hgkb_caps');
function add_hgkb_caps() {
$shop_manager = get_role( 'shop_manager' );
$shop_manager->remove_cap('moderate_comments');
}
However the same is working fine with other roles, like editor, and author.
You could try a different Method:-
add_filter( 'comment_row_actions', 'add_hgkb_caps', 15, 2 );
function add_hgkb_caps( $actions, $comment )
{
$roles=array('shop_manager'); //You can add more roles
$current_user = wp_get_current_user();
$current_user_role=$current_user->roles[0];
if(in_array($current_user_role,$roles))
{
unset( $actions['quickedit'], $actions['edit'], $actions['spam'], $actions['unspam'], $actions['trash'], $actions['untrash'], $actions['approve'], $actions['unapprove'], $actions['delete'] );
}
return $actions;
}

Automatically Deactivate Plugin After a Check

I'm trying to get my Wordpress plugin to deactivate automatically after a simple check. It seems to be calling the admin_notices method just fine, but the deactivate_plugin() method does not do anything. This is in the class constructor:
// End if the theme isn't compatible
if ( FALSE == $this->themesupport['support'] ) { // This test works fine
add_action( 'admin_init', array( &$this, 'deactivate_plugin' ) ); // Plugin doesn't deactivate
add_action( 'admin_notices', array( &$this, 'admin_notices' ) ); // I get notices
if ( isset( $_GET['activate'] ) )
unset( $_GET['activate'] );
return;
} // if()
The method is pretty straightforward:
public function deactivate_plugin() {
deactivate_plugins( plugin_basename( __FILE__ ) );
} // deactivate_plugin()
Putting an echo in that deactivate_plugin method and it gets called. I've also tried including the plugins.php file from core with no change.
The following works:
<?php
/**
* Plugin Name: (SO) Self-deactivate with $_GET['my_deactivate']
*/
add_action( 'admin_init', function()
{
if( isset( $_GET['my_deactivate'] ) )
{
deactivate_plugins( plugin_basename( __FILE__ ), true );
$url = admin_url( 'plugins.php?deactivate=true' );
header( "Location: $url" );
die();
}
});
After activating, enter any admin URL adding ?my_deactivate, e.g.:
http://example.com/wp-admin/users.php?my_deactivate
Reference:
Function deactivate_plugins does not exist
I was calling deactivate_plugins() from within a class, rather than the plugin file itself, which of course returned the wrong location for FILE

wordpress set post_status as "draft" in 'save_post' action

I have a custom function that works with my custom post type. While porocessing save_post action:
add_action( 'save_post', 'my_custom_function' );
I would like to set post status as draft (in case of a problem with getting custom data from outside api).
In my my_custom_function function I have this little block:
if ($error == true) {
$override_post = array();
$override_post['ID'] = $post_id;
$override_post['post_status'] = 'draft';
wp_update_post( $override_post );
}
but it seems, that after save_post is being processed, then post_status is being set again.
Anybody have an idea, where should I hook into, so while saving post data I can modify its post_status, post_date and some other post data informations so they are not being overriten?
You should hook it to wp_insert_post_data. Then you could use a function like this to set your post status to draft:
add_filter( 'wp_insert_post_data', 'set_post_to_draft', 99, 2 );
function set_post_to_draft( $data, $postarr ) {
if ( your_condition ) {
$data['post_status'] = 'draft';
}
return $data;
}
I had to make a post type with only one post_status option, and it seem to fit your needs too, as it is works exactly with the save_post hook.
add_action( 'save_post', 'my_function' );
function my_function( $post_id ){
if ( ! wp_is_post_revision( $post_id ) ){
// avoid endless circle
remove_action('save_post', 'my_function');
// update the data before saving
wp_update_post( wp_slash([
'ID' => $_POST['ID'],
'post_status' => 'draft'
]));
// restore the saving hook
add_action('save_post', 'my_function');
}
}
The original solution found here:
https://wp-kama.ru/function/wp_update_post

Resources