Post from front end to custom metabox - wordpress

I have a custom metabox in a custom post type. I'm using a front end posting page.
The information added by users in the form should be displayed in the post and appear in the back-end meta box in the custom meta boxe.
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['submitted'] ) && $_POST['submitted'] == "new_post") {
// Do some minor form validation to make sure there is content
if (isset ($_POST['postTitle'])) {
$title = $_POST['postTitle'];
} else {
echo 'Please enter the wine name';
}
if (isset ($_POST['postContent'])) {
$description = $_POST['postContent'];
} else {
echo 'Please enter some notes';
}
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_status' => 'pending', // Choose: publish, preview, future, draft, etc.
'post_type' => 'portfolio' //'post',page' or use a custom post type if you want to
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
wp_redirect( home_url() );
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');
get_header();
here is the form
<input type="text" id="short_description" name="short_description" style="padding: 7px; width: 100%;" value="<?php echo $short_description_no_html; ?>" />

Related

Create custom field into tag of attributes in woocommerce with add_action

I want to create a custom field into the terms of attributes in woocommerce, with add_action(), I have did search on the web but I can't found any hooks in this place.
Here where I want to add my custom field :
Woocommerce / Attributes / Configures Terms / Edit term
Have you an idea of which hooks I can use here for add my custom field ?
And another hooks to check the custom field after the form are post ?
Here a screenshot where I want my custom field :
Here the code that I want tu use (I just need the correct hook (hook1 & hook2) :
function custom_field_categorie($term)
{
$term_id = $term->term_id;
$args = array
(
'id' => 'GRE_ID',
'label' => __('ID genre'),
'class' => '',
'desc_tip' => true,
'value' => get_term_meta($term_id, 'GRE_ID', true),
'custom_attributes' => array('readonly' => 'readonly'),
);
woocommerce_wp_text_input($args);
}
add_action('hook1', 'custom_field_categorie', 10, 1);
function custom_field_categorie_save($term_id)
{
if(!empty($_POST['GRE_ID']))
{
update_term_meta($term_id, 'GRE_ID', sanitize_text_field($_POST['GRE_ID']));
}
}
add_action('hook2', 'custom_field_categorie_save', 10, 1);
Thanks for your help
There you go. Put this into your function.php file of your child theme. Tested and works:
add_action( 'product_tag_edit_form_fields', 'product_tag_edit_form_fields_action' );
function product_tag_edit_form_fields_action( WP_Term $term ) {
$term_id = $term->term_id;
if ( empty( $term_id ) ) {
return;
}
$genre_id = get_term_meta( $term_id, 'GRE_ID', true );
?>
<tr class="form-field form-required term-genre-wrap">
<th scope="row"><label for="genre"><?= __( 'ID genre', 'your-lang-id' ) ?></label></th>
<td><input name="genre" id="genre" type="text" value="<?= $genre_id ?>" size="40" readonly/></td>
</tr>
<?php
}
Since you originally added readonly to your custom args to just display the value, you don't need to save the value from this field since it can never be set empty in this form.
See the final result:

Can I set registered Custom Taxonomy For my Custom Post Type in code?

I registered Custom Taxonomy through Custom Post Type UI Plugin and assigned them for my CPT Advertisements.
When I add new Advertisement through function() after submit my form, I know set all I need (title, custom_fields...) but can I set already defined some of custom taxonomies for my new Post? I know to do that in wp-admin ... but I had to set it in code, when I choose one of the custom taxonomies from the selectbox...
Custom taxonomies assigned to my Custom Post Type:
Set Custom taxonomy through selectbox:
<select name="taxonomy-market-type" class="custom-select" id="taxonomy-category">
<?php
$terms = get_terms( array(
'taxonomy' => 'advertisement_category',
'hide_empty' => false) );
?>
<option value="" hidden>Choose category</option>
<?php foreach ($terms as $term) { ?>
<option value="<?php echo $term->slug ?>"><?php echo $term->name; ?></option>
<?php } ?>
</select>
Function, where I add New Custom Post Type:
function addAdvertisement() {
header('Content-Type: application/html;charset=utf-8');
$uploadDir = wp_upload_dir();
// Create post object
$my_post = array(
'post_title' => $_POST['name'],
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'advertisements'
);
$post_id = wp_insert_post( $my_post);
if(isset($POST['category'])) {
// add custom taxonomy here
}
// Insert the post into the database
add_post_meta($post_id, 'nazov_inzeratu', $_POST['name']);
add_post_meta($post_id, 'popis_inzeratu', $_POST['description']);
add_post_meta($post_id, 'fotka', implode(',', $uploadedImages));
add_post_meta($post_id, 'id_inzerat', $post_id);
echo get_permalink($post_id);
wp_die();
}
Is that even possible, please? Thanks for the advices
Yes, it is possible to assign custom taxonomies to custom post types by calling wp_set_post_terms() as follows.
if(isset($POST['category'])) {
$adv_cat = array($POST['category']);
wp_set_post_terms( $post_id, $adv_cat, 'advertisement_category');
}
I hope this will work.
Please have a look at this codex page for further info.

Text domain for Woocommerce Custom text area

I created a custom field in woocommerce in the general tab, as you can see here
custom textarea
I need to create the text domain for the text I will insert in the textarea , to translate it with WPML string.
I tried inserting the text domain in the placeholder
'placeholder' => __('inserire tipo calzabilità', 'woocommerce')
but it doesn't work!
What's wrong?
below the code
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
echo '<div class="product_custom_field">';
//Custom Product Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_custom_product_textarea',
'placeholder' => 'inserire tipo calzabilità',
'label' => __('Campo calzabilità', 'woocommerce')
)
);
echo '</div>';
}
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Textarea Field
$woocommerce_custom_procut_textarea = $_POST['_custom_product_textarea'];
if (!empty($woocommerce_custom_procut_textarea))
update_post_meta($post_id, '_custom_product_textarea', esc_html($woocommerce_custom_procut_textarea));
}
add_action( 'flatsome_custom_single_product_1', 'dancin_fit', 20 );
function dancin_fit() {
echo '<div class="fit"><span>' . get_post_meta(get_the_ID(), '_custom_product_textarea', true) .'</span></div>';
}

Creating a page with wp_insert_post()

I have a form that collects data about a user's product and then creates a page in WordPress with
$my_post = array(
'post_content' => "My page content",
'post_title' => $product_title,
'post_name' => $product_title,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "listing.php",
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
The form data is put into meta variables for the page ID and the listing.php template file pulls it out of there and builds the HTML to display the product page. This all works fine and I can see that the page meta variable, _wp_page_template, gets set correctly to the template file I specified, listing.php:
Now I want to create a second page from the same form data, this one displaying different parts of the data in a different way. So I've added a second block of code, starting at $my_cert below, that creates this second page and specifies a different template, certificate.php, that knows how to build the second version of the data.
$my_post = array(
'post_content' => "My page content",
'post_title' => $product_title,
'post_name' => $product_title,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "listing.php",
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
$my_cert = array(
'post_content' => "My certificate", // post_content is required
'post_title' => "My certificate", // post_title is required
'post_name' => "My certificate",
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "certificate.php",
'post_status' => "publish"
);
$CERT_ID = wp_insert_post( $my_cert );
$cert_permalink = get_permalink($CERT_ID);
echo "<br />ID for new certificate is $CERT_ID, Permalink for new certificate is $cert_permalink";
But when I look in the meta data for the second page created, the template is set to "default" instead of certificate.php:
I know I've set up certificate.php correctly as a template (set /* Template Name: certificate */ at the top) because the Page Edit Template dropdown includes certificate:
So does anyone see why I can't create this second page with the template set to certificate.php?
Thanks
Are you sure your page template src for: certificate.php is: certificate.php? And not: templates/certificate.php or something like that. Look in your theme folder and be 100% of the page template path. Check your spelling or for typos in the page template path or name. It must be an exact match.
If you still have problems I would look into and debug the source code of: wp_insert_post()
if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type'] ) {
$post->page_template = $postarr['page_template'];
$page_templates = wp_get_theme()->get_page_templates( $post );
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
if ( $wp_error ) {
return new WP_Error('invalid_page_template', __('The page template is invalid.'));
}
update_post_meta( $post_ID, '_wp_page_template', 'default' );
} else {
update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] );
}
}
So its probably this part that fails:
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) )
Try to modify: wp-includes/post.php and go to the definition of: function wp_insert_post() on row: 2872. And add a new row on row: 3312 for debugging purposes.
echo '<pre>';
print_r( $page_templates );
echo '</pre>';
die();
Make sure your certificate.php is among those in that array. Remember to delete the debug code before continuing. This should give you some answers.

Update wordpress post from front-end (using acf)

I am making front end admin for users to add/edit their posts. I already made post add form and it works, but edit form doesnt work.
functions.php
function add_new_post( $post_id )
{
if( $post_id == 'new' ) {
// Create a new post
$post = array(
'post_title' => $_POST["fields"]['field_52c810cb44c7a'],
'post_category' => array(4),
'post_status' => 'draft',
'post_type' => 'post'
);
// insert the post
$post_id = wp_insert_post( $post );
return $post_id;
}
else {
return $post_id;
}
}add_filter('acf/pre_save_post' , 'add_new_post' );
index.php
<div id="updateform-<?php the_ID(); ?>" class="collapse">
<?php
echo get_the_ID();
$args = array(
'post_id' => get_the_ID(), // post id to get field groups from and save data to
'field_groups' => array(31), // this will find the field groups for this post (post ID's of the acf post objects)
'form' => true, // set this to false to prevent the <form> tag from being created
'form_attributes' => array( // attributes will be added to the form element
'id' => 'post',
'class' => '',
'action' => get_permalink( get_the_ID() ),
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
'html_before_fields' => '', // html inside form before fields
'html_after_fields' => '', // html inside form after fields
'submit_value' => 'Update', // value for submit field
'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
);
acf_form( $args );
?>
</div>
For saving posts you should use save_post not pre_save_post.
Basically pre_save_post is used for new posts.
Use this below add_filter('save_post' , 'add_new_post');
I created plugins for that:
Forms actions
https://wordpress.org/plugins/forms-actions/
Add actions to yours ACF forms.
ACF Frontend display
https://wordpress.org/plugins/acf-frontend-display/
Display your ACF form on frontend
If by some chance you happen to be using Elementor Page Builder, you can do this by installing Advanced Widgets for Elementor. It comes with an ACF Form widget that you can just drag & drop anywhere into your site and configure your form through the UI (no coding required).

Resources