wordpress: let subscribers submit content to post ( yes / no, radio buttons ) - wordpress

i'm currently struggeling with finding the right way for a function in wordpress.
there are many subscribers who should be able to say "yes" or "no" to a post.
until now, i added some more fields to the comments template.
but its not the right way because of empty comments, its like raping the comments function.
the point is: how to let subscribers (or any other role) submit content (like a comment) to a post?
is there any other way than the_content & the_comment, user specific?

You can create custom form and after submission add data in database in wp_postmeta table.
For example `
get data from form
$data = array();
$data['user_id'] = $user->ID; // current user id
$data['post_id'] = $_POST['post_id']; // post id which you will receive after form submission
$data['answer'] = $_POST['answer']; // this is your radio button value yes or now
You can serialize $data or you can keep it as json in database.
INSERT INTO wp_postmeta (meta_key,meta_value) VALUES ('user_vote', $serialized_data);
Best Regards,
Davit.

Related

Get password value in WooCommerce checkout

Is there any way to retrieve the clean unhashed password value in the WooCommerce checkout page with any hook?
What I need to do: I need to create a Firebase Auth user when a new WordPress user is creating. If this is not possible, what would be the best practice to achieve this?
What I tried
First I tried to create a new custom field on checkout and retrieve it with:
function wh_CustomReadOrder($order_id)
{
$order = wc_get_order($order_id);
WC()->session = new WC_Session_Handler;
/*
* Next lets create a customer so we can access checkout fields
* If you will check a constructor for WC_Customer class you will see
* that if you will not provide user to create customer it will use some
* default one. Magic.
*/
WC()->customer = new WC_Customer;
/*
* Done. You can browse all chceckout fields (including custom ones)
*/
?>
<script type="text/javascript">
var order = <?php echo $order ?>;
var checkout_fields = <?php echo json_encode(WC()->checkout->checkout_fields) ?>
var email = order;
console.log(checkout_fields);
</script>
<?php
}
add_action('woocommerce_thankyou', 'wh_CustomReadOrder');
I get an array with all fields, but my custom field is not showing. But even if so, the WordPress password will still be different. The best way would be to simply get the WordPress password and then create the user in Firebase.
Do you have any idea?
As you will see in the wc-user-functions.php file, the function wc_create_new_customer is used when creating a new account.
For checking the checkout page you can use Conditional Tags
is_checkout() Returns true on the checkout page.
So to intercept the unhashed password you could use the woocommerce_created_customer hook. The $unhashed_password variable will contain the unhashed password.
function action_woocommerce_created_customer ( $customer_id, $new_customer_data, $password_generated ) {
// Returns true on the checkout page.
if ( is_checkout() ) {
$unhashed_password = $new_customer_data['user_pass'];
}
}
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 );
One approach I would consider is to build a custom webform for creating the customer's account. That way you can manipulate the data however you want.
For example, when the user submits the form, take the data, register the new user in WC/WP, send the data to firebase, then redirect.
The downside is that you'll have to manage the process a 100% and deal with any possible errors.
Another way:
Use the default WC or WP account creation form, but on submit -> prevent Default with Javascript, take the data (yes you can access the password before it's hashed), send it to Firebase, THEN, submit the form and let WC/WP save it in the database in a normal fashion.
I did it like this when I needed to send that data to an Email Management software. The user enters the values, hits submit: my code blocks the submit event, sends the data where I want it to, then submits the form.
Hope it helps!

Wordpress - Send email on Post meta value change

I want to accomplice the following. I want to automatically send an email on post publish/update when a certain post field value has changed.
In a post
A ACF field that has 4 options, lets say [ 'draft', 'ready for group1', 'ready for group 2', 'ready']
If this field gets changed on post update send email to "this" email address.
I think I need to know 2 things for this.
- How and where (what action) do I need to insert custom code on post publish/update
- How to compare new post data to old state (and is this possible/availible in the action above)
You can hook onto acf/save_post for this purpose. Read the documentation here:
https://www.advancedcustomfields.com/resources/acf-save_post/
Since you want the callback to fire before the values are stored, in order to compare the old value against the new one, keep in mind to add a priority of less than 10. Assuming the field with 4 options has the field key field_4afd4af14415f:
function on_acf_post_save($post_id) {
$post_type = get_post_type($post_id);
if ($post_type === 'your-post-type') {
$old_val = get_field('field_4afd4af14415f', $post_id);
$new_val = $_POST['acf']['field_4afd4af14415f'];
if ($old_val != $new_val) {
// Send desired mail in here:
// wp_mail(...);
}
}
}
add_action('acf/save_post', 'on_acf_post_save', 5, 1); // priority = 5
If your ACF field isn't at the top level, but inside a Group or Repeater, you will have to adapt the code reading from the $_POST['acf'] and get_field() result.

WordPress: get all meta data when user registers

I have a user registration form in the front end (in the Users admin section as well) with three extra fields (apart from default ones): birthday, country, language. their values are stored in usermeta table.
I have this action hook to retireve all meta data for the registered user:
add_action('user_register', 'new_user_func');
// user registration callback function
function new_user_func($userID) {
$newUser = get_user_meta( $userID );
$userMeta = array();
foreach ($newUser as $key => $value) {
$userMeta[$key] = $value[0];
}
//do something with $userMeta...
}
var_dump($userMeta) after submit doesn't give me the extra fields value though.. only defaults (first name, last name etc)
Anyone know what might be the case?
Did you try getting the values with:
$meta = get_the_author_meta($meta_key, $user_id);
Perhaps the meta values you add yourself isn't supported by get_user_meta() .
If this don't work either, perhaps you need to look on how you went about creating the new meta fields. Theres a pretty decent tutorial on how to do it here:
http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
Read de Codex entry for user_register action, it says:
Not all user metadata has been stored in the database when this action is triggered.

Change wordpress post author on the fly while publishing the post

I am developing my site and require to change post author id on the fly while publishing.
All post will be write and publish by admin but as content provided by different author and need to change post author id from admin to other author ( author id will get from custom field )
So how to do this on the fly while publishing.
Does the author info have to come from the custom field? Because you could do this straight in the UI manually.
Enable the "author" block in the display options when creating a post
Change the author in the drop-down menu
Here is my theme metabox.php code I am using wpalchemy metabox script and below code is in my cpt loop php file
// getting custom field value from image_artist
// this is giving value like artist_login_name / id
$png_gallery_meta->the_field('image_artist');
$artist_info = $png_gallery_meta->get_the_value();
// to separate artist_login_name and id
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );
// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;
$author_last_name = get_userdata(basename($string))->last_name;
So I am using above code to get user info from custom field dropdown selection box. Now how can I use above value to change id on the fly while admin publish the post the post author id should change with above id value get from selection box. I hope now it is more clear.
$post_id = $post->ID; // change this to whathever
$user_id = '4'; // change this too
$the_post = array();
$the_post['ID'] = $post_id;
$the_post['post_author'] = $user_id;
wp_insert_post( $the_post );
The code above will update the currently looped through post (via $post->ID) with an author with ID of 4.
Read how wp_insert_post works here. Basically, if you pass it an ID of a post that already exists it will update that post with the new information you pass to it. (the author ID in this case)
If you want to grab things from the custom fields you can use:
$author = get_post_meta($this->ID, 'author_id', TRUE);
More info on custom fields.
Does the author info have to come from the custom field? Because you could do this straight in the UI manually.
Enable the "author" block in the display options when creating a post
Change the author in the drop-down menu
Here is my theme metabox.php code I am using wpalchemy metabox script and below code is in my cpt loop php file
// getting custom field value from image_artist
// this is giving value like artist_login_name / id
$png_gallery_meta->the_field('image_artist');
$artist_info = $png_gallery_meta->get_the_value();
// to separate artist_login_name and id
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );
// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;
$author_last_name = get_userdata(basename($string))->last_name;
So I am using above code to get user info from custom field dropdown selection box. Now how can I use above value to change id on the fly while admin publish the post the post author id should change with above id value get from selection box. I hope now it is more clear.

Edit link in theme_table drupal

I have created a table in drupal to display the records. How can i add the edit link to each record so that it goes to an input form corresponding to the id for that record
function display($nid){
$query = db_query("select * from {contactus}");
$data = array();
$i = 0;
while($row = db_fetch_array($query)){
$data[$i] = $row;
$i++;
}
$output = theme_table(array('id','email','comment'),$data);
return $output;
}
You must implement the full CRUD-range, Create Read Update Delete. Right now you only have an index. For Drupal7 there is a good example in dbtng (from the examples)
For Drupal 6 I am not aware of such an example.
Basically the pattern is:
make hook_menu-items with callbacks, one for the index, Read, Update, Delete, Create.
The Read item simply shows the item (item/%id)
The Update shows a form to update the item (item/%id/edit). Form is pre-filled. See FormApi in Drupal for more information on forms.
The Delete shows a confirm_form() with a callback to delete the entry from the database.
The Create shows a form to create a new item. Form is empty.
But to answer your exact quesion, in Drupal you create a link with l(). l('foo', 'item/bar') Will create a foo.

Resources