I have a site with over 20,000 users. Occasionally I need to update the author of a post. However, the sheer amount of users makes the "Author" metabox on the edit post screen unusable. I'm trying to make a custom box where I can input the desired author's ID and change the post author to that ID. Ideally, I would like to do this within the box itself, rather than by way of saving/updating the post.
Here's my custom meta box code:
//Get the post data
$id = get_the_ID();
$author_id= $post->post_author;
<div class="update-author">
<p><strong>Current Author ID:</strong> <?php echo $author_id;?></p>
<p><strong>CHANGE AUTHOR ID TO:</strong></p>
<form id="update-author-form" method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" >
<input type="hidden" name="action" value="update_author_form">
<input type="hidden" name="post-id" value="<?php echo $id; ?>">
<input type="number" id="update-author-id" name="update-author-id" value="" />
<button id="update-author-button" class="button button-primary button-large">Update Author ID</button>
</form>
</div>
Next, I have my form function built as follows:
function handle_update_author_form() {
$post_id = $_POST['post-id'];
$update_id = $_POST['update-author-id'];
$my_post = array(
'ID' => $post_id,
'post_author' => $update)_id,
);
wp_update_post( $my_post );
}
At this stage, my form button does nothing but refresh the page and send it to the posts screen (I know, I need to change the redirect URL).
Like in comments - I would go for a another approach of removing the original meta_box and replacing it with a new one - or alternatively just use a JS to turn the dropdown list into a searchable field ( for example the excellent select2 plugins - but have others - and also pure JS is quite easy - see example below )
Anyhow - in your code that you posted you are missing the hook where you can apply and invoke your code, for example:
'add_action( 'save_post', 'handle_update_author_form', 10, 1 ); '
Have a look at wp filters and actions.
The suggested (simple) JS solution :
Now, for the simple approach of just using JS to pseudo-search inside the dropdown and therefor there is no real need to interfere with the query or post injections.....
myPlugin.php / myTHeme,php
function admin_enqueue_scripts_callback(){
// We are using select2.js from CDN here ..
//This will add Select2 CSS
wp_enqueue_style( 'select2-css', 'https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/css/select2.min.css', array(), '4.1.0-rc.0');
////This will add Select2 JS
wp_enqueue_script( 'select2-js', 'https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/js/select2.min.js', 'jquery', '4.1.0-rc.0');
// Now we need a JavaScript file to initialize the Select2 elements
// here it is staged as a plugin - but a theme would be the same .
wp_enqueue_script( 'select2-init', '/wp-content/plugins/select2-init.js', 'jquery', '4.1.0-rc.0');
}
add_action( 'admin_enqueue_scripts', 'admin_enqueue_scripts_callback' );
select2-init.js
// initialize select2 on author dropdown field with jQuery
jQuery(document).ready(function($) {
$('#post_author_override').select2();
});
voilĂ !
At this point you will have the pseudo-search implemented that would resolve your problem in a practical way without any new ->queries or $objects and with minimum coding.
Possible / potential caveats
I am not sure what would be the efficiency of filtering 20,000 items.
It would be interesting to know how the select2 JS would perform with such a long list...
But if it is already loaded to the DOM i guess it would be ok ( might take a few seconds to react though )
Please let me know how it performing if you implement the solution.
Related
I've added a new field onto the discussion's section on the /wp-admin/options-discussion.php page that outputs along with this:
<?php do_settings_sections('discussion'); ?>
How can I hook the Save Changes button so that I can handle saving my new setting's fields?
I've tried updated_option like so:
add_action('updated_option', [__CLASS__, 'sanitize_settings'], 11, 3);
But I don't think this is what I need.
for saving data in wp_options you should set the name and value together
for example first we can add new options with add_option function and see this link
add_option( 'nameOfYourOption', 'value', '', 'yes' );
then you can easily access your data with the get_option function and see this link
$yourOption = get_option( 'nameOfYourOption' );
var_dump($yourOption)
and if you want to change the value you can do that with update_option function and see this link
update_option( 'nameOfYourOption', 'newValue' );
With these three functions, you can manage your option
but if you try just using the hook for update_option see this link
do_action( 'update_option', string $option, mixed $old_value, mixed $value )
Updated
When you try to click on a Save button to using those functions, you can handle that with a form or with WordPress ajax
<form method="post">
<button name="uniqName" type="submit">Save Changes</button>
</form>
in wordpress/php
<?PHP
if(isset($_POST['uniqName'])){
//do something
}
and if you try to send data with ajax, just see this link
Hi I've been trying to get my "update cart" button to work on the cart page made by woocommerce. It is on my own theme and I have added the cart.php template to it. In the woocommerce template it had quantity but it didn't have an easy to use add more or less buttons. So I edited the global/quantity-input.php to have this trait. I have tested the single-products pages to use my new quantity and adding to cart. This works great. But on the cart page the update cart button is disabled unless a change is made, and it doesn't recognize my changes.
Things I've tried that worked:
When I manually go into the inspector and remove the "disable" attribute from the button
When I press "enter" on my keyboard in the quantity input after i press the "-" or "+" button.
Plus and minus do change the quantity in the input field
Typing in the quantity enables the update cart button
Things I've tried that did not work:
Using javascript to target the button and do 'update_cart.disable = false;'
Tried calling a submit on change to automatically adjust the cart
Just simply hope that the form recognizes the quantity change via my buttons
My code for the Javascript
function minus_quantity(e) {
var this_input = this.parentNode.querySelector('input[type=number]');
var current_val = this_input.value;
var new_val = parseInt(current_val) - 1;
this_input.value = new_val;
document.getElementsByName('update_cart').disable = false;
e.preventDefault();
}
function add_quantity(e) {
var current_val = this.parentNode.querySelector('input[type=number]').value;
var new_val = parseInt(current_val) + 1;
this.parentNode.querySelector('input[type=number]').value = new_val;
document.getElementsByName('update_cart').disable = false;
e.preventDefault();
}
My code inside cart.php
<button type="submit" class="button" name="update_cart" value="<?php esc_attr_e( 'Update cart', 'woocommerce' ); ?>"><?php esc_html_e( 'Update cart', 'woocommerce' ); ?></button>
<?php do_action( 'woocommerce_cart_actions' ); ?>
<?php wp_nonce_field( 'woocommerce-cart', 'woocommerce-cart-nonce' ); ?>
My HTML code inside the browser
<button type="submit" class="button" name="update_cart" value="Update cart" disabled="">Update cart</button>
<input type="hidden" id="woocommerce-cart-nonce" name="woocommerce-cart-nonce" value="6090857223">
<input type="hidden" name="_wp_http_referer" value="/cart/?purge_success=1&cache_type=all"><!-- </div>-->
I should mention that my selector on the button isn't working and I don't know why.
Also, I am very new to woocommerce and can't find anything on this topic. Thank you for reading this.
UPDATE 1:
I found this ticket and tried it
Woocommerce 2.6.2 adds disabled attribute to update cart button
I added this to the bottom of my javascript function ( I did make sure it's in a Jquery wrapper btw)
$('button[name="update_cart"]' ).removeProp( 'disabled');
However this did not work either. I used the inspector and it appears to not get anything back with the selector, but when I console log it, I get something back.
The reason why your javascript isn't working is because of two reasons:
The attribute you need to change is not "disable", it should be "disabled".
You are selecting your button from the DOM using getElementsByName, which actually returns an array of all the elements with that name in your page. The way you would access it and change the disabled attribute (assuming you only have one element with that name) would be like this:
document.getElementsByName('update_cart')[0].disabled = false;
However that is bad practice since you could have more than one element with that name and thus I would suggest giving your button an id and then using getElementById instead. You wouldn't need to add the [0] in this case.
I'm attempting to save a simple text input to the WooCommerce session. The session is created when a user adds something to their cart.
My input field exists in custom page template that will be placed in the user flow after the cart but before the checkout: cart > my template > checkout.
So far
Simple form to capture data (custom template file)
<form name="group" method="post" class="checkout woocommerce-checkout" action="http://localhost:/site.dev/my-template">
<div class="group-order">
<p class="form-row form-row woocommerce-validated" id="create_new_group_field">
<label for="create_new_group" class="">Join an existing group</label>
<input type="text" class="input-text " name="create_new_group" id="create_new_group">
</p>
</div>
</form>
Receiving and setting data (I'm having trouble figuring out when/how to run this. in my custom page)
UPDATE
I've added the code below to the top of my page template so the page processes itself and then re-directs to the checkout.
function set_and_save_input_to_session() {
if( !is_admin( ) ) {
// User input
if( ! empty( $_POST['create_new_group'] ) ) {
$group_input_value = $_POST['create_new_group'];
// Set session and save data
WC()->session->set( 'group_order_data', $group_input_value );
wp_redirect( 'http://localhost:28/site.dev/checkout' );
exit();
}
}
get_header();
add_action('woocommerce_checkout_process', 'set_and_save_input_to_session');
Retrieving and saving data
function retrieve_and_save_group_input_value_to_order_meta() {
$retrived_group_input_value = WC()->session->get( 'group_order_data' );
update_post_meta( $order_id, '_create_new_group', $retrived_group_input_value );
}
add_action('woocommerce_checkout_update_order_meta', 'retrieve_and_save_group_input_value_to_order_meta');
I'm currently working my way through what are to me, more complex solutions and therefore I'd appreciate if anyone could point out any major flaws with my process so far.
UPDATE
I can confirm that the form is receiving data and that the WC()->session->set is setting data. (Thanks to #Firefog for suggesting the use the $_SESSION global)
After further investigation and finding the right place to var_dump the session data I found that the data was being set to the session with my original method.
The data is set, but I can't see why the data won't save to the order.
It's more saying Thank you for solving my problem. But here is an answer, too:
The post meta could not been updated because there is no $order_id parameter in your callback function. This should do the trick:
function retrieve_and_save_group_input_value_to_order_meta( $order_id ) {
$retrived_group_input_value = WC()->session->get( 'group_order_data' );
update_post_meta( $order_id, '_create_new_group', $retrived_group_input_value );
}
add_action('woocommerce_checkout_update_order_meta', 'retrieve_and_save_group_input_value_to_order_meta');
Here is another approach.
1st page:
session_start();//place this at the top of all code
$data = $_POST['create_new_group'];
$_SESSION['custom_create_new_group']=$data;
Now in another page write the following to receive the value:
session_start(); //optional
$retrive_price = $_SESSION['custom_create_new_group'];
I have a list (shown as a grid by this plugin) of Custom Post Types, but I need the ones under one taxonomy value to have a link to the inner page.
Example: I have the taxonomy "example" with 3 options (OptionA
- OptionB
- OptionC), but I just want the ones under "OptionB" to have an inner link:
I know there is a solution via css (hiding the links styles), but I would like to keep the whole site clean of css tricks.
Is there any way to achieve this functionality using PHP?
Here is the part of the PHP code that adds the link to the titles:
$output .= '<div class="pl-detailcnt">
<h4 class="pl-title left-txt">';
if (isset($this->pw_hide_date) && ($this->pw_hide_date=='off')){
$output .= '<span class="pl-date">'. get_the_date($this->pw_date_format).'</span>';
}
$output .= ''. get_the_title().'</h4>
</div>';
Since I can't add the whole code (max characters exceeded) as #Dontfeedthecode suggested, here it goes: http://ideone.com/HeVfny
You can query for taxonomies for the post then check to see if your custom post type is in the array it returns:
$post_types = get_object_taxonomies( $post );
if( in_array( 'your taxonomy name', $post_types )) {
// Show link
}
SOLUTION (using a bit of JQuery).
First step:
Add the term-slug as a CSS class, so I can distinguish between classes for customize it later with JQuery.
<div class="add_your_random_class_here '.$term->slug.'">
Second Step:
Disable the links that have the term-slug class created.
<script>
jQuery(function() {
jQuery('.here-goes-your-new-based-slug-class').click(function(e){e.preventDefault();});
});
</script>
Any improvement will be welcome
I have input fields with names like: child1, child[2], child[3] and so on. A user can add as many fields as he wants, its handled with jquery and dynamically ads an input field with [n] in the end of the name.
Now I need to store this data to database in WordPress plugin I work on. And later retrieve this data to display it on the website, and of cause add those field out fields in admin page, where user edits data.
I know how to store one field for example child to the database in WordPress, it would be something like that:
<?php $child = get_post_meta( $post->ID, 'child', true ); ?>
<?php
function save_child_data( $post_id ) {
// Check if exists
if(! isset($_POST['child'])){return;}
// Sanitize
$child_data = sanitize_text_field( $_POST['child'] );
// Store data
update_post_meta( $post_id, 'child', $child_data);
}
add_action( 'save_post', 'save_child_data' );
?>
<!-- Field in admin -->
<input type="text" name="child" id="child" value="<?php echo esc_attr($child); ?>">
<!-- Front office -->
<div>Child: <?php echo get_post_meta( $post->ID, 'child', true ); ?></div>
So my question is how do I handle this array like child[n]?
Or another question maybe it is better to just use child1, child2, child3 and handle it as regular input fields?
So you understand what I mean by dynamically added block here is a jsfiddle link: http://jsfiddle.net/alexchizhov/LC2K6/
Store it as an JSON or serialized string in one field.
http://ch1.php.net/manual/en/function.json-encode.php
http://ch1.php.net/manual/en/function.serialize.php