Resetting all the fields under the same agency/ author
-
Is there the way to clean/reset all the price fields of the apartments/posts under a certain agency/author? I could set a “price reset button” in the author area
The page I need help with: [log in to see the link]
-
What plugin are you using to manage the price fields?
they are all custom fields
Then your solution could work. You’d have to write or extend a custom plugin.
It’s possible to do an UPDATE query in SQL to set all meta values to 0 where the meta key is “price” (or whatever) and the related post’s author ID is X. Because the postmeta table doesn’t have post author information, you’ll need to join the wp_posts table so author data is available to the query.
You can add a button to initiate such a query in a user’s profile screen. There are a number of possible action hooks you could use. The right one depends on where in the profile screen you want the button to appear.
The button could trigger an Ajax request telling the server to execute the above mentioned UPDATE query for the user ID passed in the Ajax request. The script could respond with success or failure and the Ajax script could then display an appropriate message in the user profile screen.
This can all be incorporated into your existing custom theme or plugin.
I’d like to put the button in this area https://test.sacconicase.com/wp-admin/user-edit.php?user_id=5&wp_http_referer=%2Fwp-admin%2Fusers.php%3Frole%3Dauthor
I have already created some custom fields from this position. I could start with the HTML?
But value= 0 is equal to “no value”?
Look through the source code of user-edit.php. Any place
do_action()
is called is where you could output a button. For example, output from ‘personal_options’ action will occur just above the “Name” section. Hook the preferred action and echo out any HTML you like. You’ll eventually add a JS listener to the element, which will be easier if the element has its own unique ID attribute.But value= 0 is equal to “no value”?
Depends on the context. In some cases
0
is a value, or it could evaluate tofalse
. If you prefer, the field could be set tonull
(the wp_postmeta default for meta_value), or the entire record could even be removed if that made sense.I started with a simple html:
<td> <button type="button">Cancella prezzi</button> </td>
The purpose is to erase all the data of a series of fields. Here is the example od the update of one of these fields
update_post_meta( $post_id, "function_prezzo_primo", $number );
these fields are situated in the post editor page, instead the button is in the edit author page
There’s no post data within scope of user-edit.php action hooks. All you can do there is output button HTML. Before doing so, it’d be a good idea to check the user’s capabilities. If they are unable to author posts or have not authored any, it makes no sense to have a cancel button in their profile. Use typical add filter/action hook code, for example:
add_action('personal_options', 'sac_cancel_button'); function sac_cancel_button( $user ) { // verify user capabilities and if they've authored any posts // echo out whatever HTML you want here }
You’d next add a JS click event listener that sends a request to the server that basically says “User X wants all their posts canceled”. Before doing so, it’s probably a good idea to ask the user “Are you sure you want to cancel all posts?” There is likely no chance of recovery once the action is taken.
Once the server receives the request, it should verify the current user has authority to cancel a user’s posts. Some sort of nonce verification is also important. Once everything checks out, query for that user’s posts and do what is necessary to cancel their posts. Finally, respond to the request telling your JS script whether the cancellation was successful or not. An appropriate message can then be added to the profile screen.
While you’re outputting the button in the profile screen, it’ll be useful to also include a div container for the response message. It can initially be empty, it’s only populated when the server sends a response.
My html button at the moment is already inside a function:
add_action( 'show_user_profile', 'my_show_extra_profile_fields' ); add_action( 'edit_user_profile', 'my_show_extra_profile_fields' ); add_action( "user_new_form",'my_show_extra_profile_fields' ); function my_show_extra_profile_fields( $user ) { ?>
If the button already appears where you like, then the next step is to add a JS click event listener. A lot of coders prefer to use jQuery for this because it’s a little simpler. The WP back end already has jQuery loaded, so it’s available whether you use it or not.
https://api.jquery.com/click/Once a click occurs, send an Ajax request. The PHP Ajax handler then does whatever needs to be done.
This is my button
<td> <button type="button" ID="target">Cancella prezzi</button> </td>
Should I add this?
$( "#target" ).on( "click", function() { alert( "Sicuro di voler cancellare i prezzi?" ); } );
it’s not clear to me where I have to put the code and which tags I have to use, if I simply write the code inside the <td> I just output the text
That would be a start. Getting an alert box to appear when you click your button will prove everything is working up to that point. Then you can start building upon that to eventually get what you really want.
It may seem a little silly for only 3 lines of jQuery code, but I recommend placing it in its own .js file that is kept within your plugin or theme. More code will be added in the future where it’ll then make more sense. You may as well start now.
To get the file loaded onto the user-edit.php page, you’ll need to enqueue it.
You will enqueue the file from within a callback function added to “admin_enqueue_scripts” action hook. Note that $hook_suffix is passed to your callback. You can verify that its value is “user-edit.php before enqueuing to avoid needlessly loading it onto other pages.
WP jQuery runs in noConflict mode, so to use the
$
shortcut you’ll need to us a “noConflict wrapper” that looks like this:(function( $ ) { "use strict"; $(document).ready( function(){ // your click event listener code goes here } ); })(jQuery);
Also included is document ready code that causes your code to only run after the page has fully loaded. Otherwise it’ll try and fail to find the #target element before it actually exists on the page.
I create areyousure.js and put inside it
(function( $ ) { "use strict"; $(document).ready( function(){ $( "#target" ).on( "click", function() { alert( "Sicuro di voler cancellare i prezzi?" ); } ); } ); })(jQuery);
if it’s correct I’ll put in the folder js inside the theme
Looks OK, but is untested. Placing the file in your theme will not accomplish much of anything yet, but do it anyway. It will need to be enqueued before anything has an effect.
FYI, we check for JS and jQuery errors by looking at our browser’s console for any error messages. Access in most browsers is with Ctrl+Shift+I, then pick the Console tab if it’s not already current.
Now I put this code inside the html of the button?
<?php function sacconi_load_admin_scripts(){ wp_enqueue_media(); wp_register_script('sacconi-admin-script',get_template_directory_uri() .'/js/areyousure.js', array('jquery'), '1.0.0', true); wp_enqueue_script('sacconi-admin-script'); } add_action( 'admin_enqueue_scripts', 'sacconi_load_admin_scripts' );
- The topic ‘Resetting all the fields under the same agency/ author’ is closed to new replies.