For printing do I need to create a text field in the form?
if ( (! isset($_POST["wizpart[sku]"])) || (! isset($_POST["wizpart[quant]"])))
//if ( (empty($_POST["wizpart[sku]"])) || (empty($_POST["wizpart[quant]"]))) {
//if (! isset($_POST["wizpart[sku]"], $_POST["wizpart[quant]"])) {
print '<p class= "error">Fields not completed!</p>';
$okay = FALSE;
}
Complete code
<?php
add_action( 'add_meta_boxes', 'wizpart_meta_box_init' );
// meta box functions for adding the meta box and saving the data
function wizpart_meta_box_init() {
// create our custom meta box
add_meta_box( 'wizpart_meta', // Unique ID
'Parts Information', // Title
'wizpart_meta_box', // Callback function
'product', // post type
'normal', // Context
'high' // Priority
);
}
function wizpart_meta_box( $post ) {
// Leading HTML
// retrieve the custom meta box values
$wizpart_meta_full = get_post_meta( $post->ID, '_wizpart_data');
echo var_dump($wizpart_meta_full).'<br />';
if (! empty($wizpart_meta_full)) {
foreach( $wizpart_meta_full as $entry){
// print out each part
print 'Product comprises '.$entry["quant"].' units of '.$entry["sku"].' <br />';
}
}
//nonce for security
wp_nonce_field( 'meta_box_save', 'wizpart_plugin' );
// Form HTML
echo '<div class = wizpart>';
echo '<table>';
echo '<tr>';
echo '<td> Enter correct SKU of part: </td>';
echo '<td> <input type="text" name="wizpart[sku]" value = "" size="5" > </td>';
echo '</tr>';
echo '<tr>';
echo '<td> Enter quantity of parts required as integer: </td>';
echo '<td> <input type="text" name="wizpart[quant]" value = "" size="5" > </td>';
echo '</tr>';
echo '</table>';
echo '</div>';
}
// hook to save meta box data when the post is saved
add_action( 'save_post', 'wizpart_save_meta_box' );
function wizpart_save_meta_box( $post_id ) {
// Flag variable
$okay = TRUE;
// catch incomplete form
if ( (! isset($_POST["wizpart[sku]"])) || (! isset($_POST["wizpart[quant]"])))
//if ( (empty($_POST["wizpart[sku]"])) || (empty($_POST["wizpart[quant]"]))) {
//if (! isset($_POST["wizpart[sku]"], $_POST["wizpart[quant]"])) {
print '<p class= "error">Fields not completed!</p>';
$okay = FALSE;
}
// if auto saving skip saving our meta box data
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
//check nonce for security
wp_verify_nonce( 'meta_box_save', 'wizpart_plugin' );
// store data in an array
// Get the post ID from the from SKU
$wizpart_data = $_POST['wizpart'];
// use array map function to sanitize options
$wizpart_data = array_map( 'sanitize_text_field', $wizpart_data) ;
// save the meta box data as post meta using the post ID as a unique prefix
// using add_post_meta to increment the array with values
if ($okay) {
add_post_meta( $post_id, '_wizpart_data', $wizpart_data );
print '<p class= "error">Success!</p>';
} else {
print '<p class= "error">Encountered error</p>';
}
}
?>
]]>/*
Plugin Name: WIZ Custom Meta Box Plugin
Plugin URI:
Description:
Version: 1.0
Author: David
Author URI:
License:
*/
add_action( 'add_meta_boxes', 'wizpart_meta_box_init' );
// meta box functions for adding the meta box and saving the data
function wizpart_meta_box_init() {
// create our custom meta box
add_meta_box( 'wizpart_meta', 'Parts Information', 'wizpart_meta_box', 'product', 'normal', 'high' );
}
function wizpart_meta_box( $post ) {
// retrieve the custom meta box values
$wizpart_meta = get_post_meta( $post->ID, '_wizpart_data');
// Debug $wizpart_meta = array("sku"=>"GA1", "quant"=>"6");
echo var_dump($wizpart_meta);
$wizpart_sku = (! empty( $wizpart_meta[0]))? $wizpart_meta[0] : 'NA';
$wizpart_quant = (! empty( $wizpart_meta[1]))? $wizpart_meta[1] : 'NA';
//nonce for security
wp_nonce_field( 'meta_box_save', 'wizpart_plugin' );
// display stored values
echo '<table>';
echo '<tr>';
echo '<td> SKU: </td>';
echo '<td> <input type="text" name="wizpart[sku]" value = "'.esc_attr( $wizpart_sku ).'" size="5" > </td>';
echo '</tr>';
echo '<tr>';
echo '<td> Quant: </td>';
echo '<td> <input type="text" name="wizpart[quant]" value = "'.esc_attr( $wizpart_quant ).'" size="5" > </td>';
echo '</tr>';
echo '</table>';
}
// hook to save our meta box data when the post is saved
add_action( 'save_post', 'wizpart_save_meta_box' );
function wizpart_save_meta_box( $post_id ) {
// process form data if $_POST is set
// if auto saving skip saving our meta box data
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
echo "<h2>PHP is Fun!</h2>";
//check nonce for security
wp_verify_nonce( 'meta_box_save', 'wizpart_plugin' );
// store data in an array
$wizpart_data = $POST['wizpart'];
// use array map function to sanitize options
$wizpart_data = array_map( 'sanitize_text_field', $wizpart_data) ;
// save the meta box data as post meta using the post ID as a unique prefix
// using add_post_meta to increment the array with values
foreach( $wizpart_data as $value ) {
echo $value;
add_post_meta( $post_id, '_wizpart_data', $value );
}
}
The ‘PHP is Fun!’ line is not displayed. No data is entered under ‘_wizpart_data’ in the DB. Any ideas – I am relatively new to PHP
]]>If saving a post where only the categories or tags are changing, the term_relationships
table (and/or other term-related tables) are not updated until after save_post
is triggered. If the post is being saved with any changes in addition to the category or tag (e.g., the title or content), then the term_relationships are updated before save_post
is called.
e.g., If I change only the category and click “update” my save_post
callback-function’s query to the database doesn’t see the new category assigned to this post (it looks like it did before the change since the category was the only update). If I then make a 2nd change to only the category in that post, the query retrieves the previous change, but doesn’t see the latest change. If I then change the title of the post only (no category change), the query retrieves the final change in category (and the change in title). If I change the title and the category together, I see the category and title change right away.
I’m using PDO to query the database directly not wp_query()
.
If anyone has any ideas of how to get the updated values right away, that would be great.
]]>
add_action( 'add_meta_boxes', array( $this, 'add_custom_metaboxes' ) );
add_action( 'save_post', array( $this, 'handle_section_clone' ) );
/**
* Add_custom_metaboxes
*
* @return void
*/
public function add_custom_metaboxes() {
add_meta_box( 'gwn-section-selector', 'Select Sections from other Courses', array( $this, 'section_selector_metabox_content' ), LP_COURSE_CPT, 'normal' );
}
/**
* Sesson_selector_metabox_content
*
* @param mixed $post Post object.
* @return void
*/
public function section_selector_metabox_content( $post ) {
// Add the nonce field.
wp_nonce_field( 'gwn_section_selector_nonce_action', 'gwn_section_selector_nonce' );
// Require the template file.
require plugin_dir_path( __FILE__ ) . 'templates/clone-sections-metabox.php';
}
/**
* Handle section clone.
*
* @param int $post_id Post ID.
*/
public function handle_section_clone( $post_id ) {
var_dump($_POST); exit;
}
]]>add_action( 'dp_duplicate_post', 'auto_generate_title_copy_post' );
function auto_generate_title_copy_post( $new_post_id ) {
$post = get_post( $new_post_id );
$post_type = $post->post_type;
if( $post_type == "ratings" ) {
// get org tax
$orgs = wp_get_post_terms($new_post_id, 'org');
$org_name = $orgs[0]->name;
// get weightclass tax
$wcs = wp_get_post_terms($new_post_id, 'weight-class');
$wc_name = $wcs[0]->name;
// get date
$date_data = get_field("rating_month", $new_post_id);
if($date_data) {
$date = DateTime::createFromFormat('Ymd', $date_data);
$date_string = $date->format("m/Y");
} else {
$date_string = "Resave to generate a new title";
}
// update title
$new_title = strtoupper($org_name . ": " . $wc_name . " - " . $date_string);
// new post args array
$post_update = array(
'ID' => $new_post_id,
'post_title' => $new_title
);
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'set_generated_post_titles');
// update the post
wp_update_post( $post_update );
// re-hook this function
add_action('save_post', 'set_generated_post_titles');
}
}
I have a custom post type called ratings which has a generated post title based on taxonomies and other custom fields. I used the WP hook ‘save_post’ to generate the title based on information entered. When I duplicate a rating, the title is empty. Above you can see a copy of the code that I used in the ‘save_post’ hook, but used in the hook ‘dp_duplicate_post’. The only change was from $post_id to $new_post_id.
I have another custom post type that generates titles with only custom fields and the duplicate function seems to work fine without any additional code for the generated title. Does using taxonomies cause errors here?
]]>My save_post is like this:
add_action( 'save_post', 'ac_check_link' );
function ac_check_link($post_id) {
remove_action( 'save_post', 'ac_check_link' );
update_post_meta( $post_id, 'ac_link_checkbox', $_POST['ac_link_checkbox']);
if ($_POST['ac_link_checkbox'] != "no") {
$post = get_post( $post_id );
$content = $post->post_content;
// do stuff
wp_update_post( [
'ID' => $post_id,
'post_content' => $content
] );
}
add_action( 'save_post', 'ac_check_link' );
}
Regardless if the checkbox is selected or not, everything inside the if runs. I tried getting the meta data with get_post_meta but it also doesn’t work.
if I var_dump and exit the if statement, in the network when I try to update I see that the request in “wp-json/wp/v2/posts/64?_locale=user” returns true and “wp-admin/post.php?post=64&action=edit” returns false, but I don’t know what this means.
Any idea how I can make this work? Thanks!
]]>I nhave a acf that is filter to be filled after a specific category is selected.
I need to get this field data on wp save_post hook, I’d even try the wp_after_insert_post hook, booth withouth succes.
Searching answers I’d find a lot of references to the ‘acf/save_post’ hook.
I just try to get what data this hook have but with no success either.
The code to test the acf/save_post hook on my functions.php theme file:
function test_acf($post_id) {
error_log( 'Saved post ID : ' . $post_id, false);
error_log( var_export($_POST, true), false );
//If you want to log what got saved to the database
error_log( var_export(get_post($post_id), true), false );
}
add_action('acf/save_post', 'test_acf', 20);
]]>I am working on a WordPress Multisite, and I am trying to achieve the following:
When posting a post on Blog 1, I distribute this post to Blog 2, Blog 3 etc.
This all works perfectly, EXCEPT for the YOAST metadata. When I add Yoast Metadata for example the title or the meta description, and i click ‘update’, the metadata disappears. Please check the code below. I have no clue what’s going wrong here:
function update_content($post_id){
if (get_current_blog_id() == 1 ){
//Check it's not an auto save routine
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
$post = get_post($post_id);
$title = $post->post_title;
// $slug = get_page_by_path($post->post_name);
$name = $post->post_name;
if($post->post_type == 'post' ) {
$original_id = $post_id;
$original_post = get_post($original_id);
// USA
switch_to_blog(2);
// $new_post = get_page_by_title( $title, 'OBJECT', 'post' );
$new_post = get_page_by_path( $name, 'OBJECT', 'post');
$new_post_id = $new_post->ID;
$my_post = array(
'ID' => $new_post_id,
'post_author' => $original_post->post_author,
'post_date' => $original_post->post_date,
'post_modified' => $original_post->post_modified,
'post_content' => $original_post->post_content,
'post_title' => $original_post->post_title,
'post_excerpt' => $original_post->post_excerpt,
'post_status' => $original_post->post_status,
'post_name' => $original_post->post_name,
'post_type' => $original_post->post_type
);
remove_action( 'save_post', 'update_content', 20, 3);
if($new_post_id){
wp_update_post( $my_post );
} else{
wp_insert_post( $my_post );
}
add_action('save_post', 'update_content', 20, 3);
switch_to_blog(1);
}
}
}
add_action('save_post', 'update_content', 20, 3);
I would really appreciate it when someone could point me towards the right direction.
]]>frontend_admin/save_post
dont redirect after saving the entry. I got error undefined
(in red block over form) and nothing happend.function.php
add_action('acf_frontend/save_post', 'my_fa_save_post', 10, 2);
function my_fa_save_post( $form, $post_id ) {
$return_id = $form['record']['post']; // value ok
$log_type = get_field('log_type', $post_id); // value ok
switch ($log_type) {
case 'Planta':
$return_id = get_field('log_planta', $post_id); // value ok
break;
case 'Ambiente':
$return_id = get_field('log_ambiente', $post_id); // value ok
break;
default:
$return_id = $post_id; // value ok
break;
}
wp_redirect(get_permalink($return_id)); // value ok
exit;
}
In the form, a post is chosen to relate to and redirect to when the form is submitted.
log_type
is a conditional for log_planta
and log_ambiente
log_planta
or log_ambiente
return an post ID
Permalink url is ok then
Browser console > network i see the redirection call, but not happened