Wordpress save metabox checkboxes selection - wordpress

Having an issue whereby the checkboxes inside a metabox aren't saving. I can save one value and have it return the value of checked to the checkboxes, just not multiple values, which I need. I've tried adding a foreach loop to the update_post_meta code but it didn't work. Slightly confused as to where I'm going wrong.
checkbox code is:
$areas = $wpdb->get_results("SELECT * FROM locations ORDER BY locationName ASC");
if( count($areas) ) :
?>
<div id="locationAssignedBoxes" size="1">
<?php
foreach($areas as $area) :
?>
<input type="checkbox" id="locationAssigned" name="locationAssigned" value="<?php echo $area->id; ?>"<?php if(get_post_meta($post->ID, 'locationAssigned', true) == $area->id) { ?> checked="checked"<?php } ?> /> <?php echo $area->locationName; ?><br>
<?php
endforeach;
?>
</div>
<?php
endif;
?>
Update_post_meta code is:
update_post_meta($post->ID, 'locationAssigned', $_POST['locationAssigned']);
Thanks very much!

This isn't a cut-and-paste answer to your question, but it should help. (I'm trying to solve a similar problem.)
Your problem in general: update_post_meta() saves a single meta value, not a collection of values. Because you want to save the values of multiple checkboxes, you have two choices:
Call update_post_meta() once for each of the checkboxes, creating a
collection of meta values associated with the post.
Combine all of the checkbox values into a single string and save those as a single value with one update_post_meta() call.
These two earlier questions are related and might point you in the right direction.
Save checkboxes of a metabox with a foreach loop (invalid argument)
Listing Pages With Checkboxes In a Metabox (and saving them)

Related

How to show multiple Google Maps with different locations created in Advance Custom Fields Wordpress plugin?

I have created two Maps fields in Advanced Custom Fields (ACF) Plugin in its field group area, please check the screenshot
But on the front end when I try to add the code as per Google Maps custom field code only one Map is showing not the second one https://www.advancedcustomfields.com/resources/google-map/
Ah yes, I'm able to figure this out. There has no need of any other different settings, just need to add one more field in ACF fields group area and show that field in the front end by doing the same settings as did on the first field but just change the location variable name and pass the second field value in it. Below is the complete code example to show the first and second map in different HTML elements.
First Map:
<?php
$location = get_field('map');
if( !empty($location) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
</div>
<?php endif; ?>
Second Map:
<?php
$location_second = get_field('map_second');
if( !empty($location_second) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location_second['lat']; ?>" data-lng="<?php echo $location_second['lng']; ?>"></div>
</div>
<?php endif; ?>
As I have created two map fields in ACF plugin, first map field's name is Map and Second map field name is Map Second, so these will be used as map and map_second in code as given above.
I hope you have ACF Pro version :)
then you can use repeater field
Create repeater field "locations" then inside only one field with name "location" and use code from example "Render multiple markers onto a map"

Pass dynamically generated select box name into $_POST array

To make an WordPress widget I created a select box which gets options generated dynamically from $post object. The select box's name is also generated by dynamically. Here is the code
<select class="widefat" name="<?php echo $this->get_field_name( 'employee' );?>" id="<?php echo $this->get_field_id( 'employee' ); ?>">
Results this :
<select class="widefat" name="widget-employee[13][department]" id="widget-employee-13-department">
To check the value of the select box I need to pass it to $_POST Array.
How do I pass the select box's value to $_POST array ?
Thanks in advance.
On submit, the $_POST variable will contain all the input fields/selected values etc. from your form in it. You don't have to manually add anything. You can manually pull info from your $_POST variable if you wish.
You said you want to check $_POST against your value ($post->post_title), in that case, since your select has a name
widget-employee[13][department]
You'll use something like
$_POST['widget-employee'][13][department]
The best bet is to just use print_r($_POST) to see the contents of your $_POST variable and see what is in it, so that you can check against.

Advanced Custom Fields/PHP issue

I apologize if this is answered elsewhere, but I'm having an issue with this ACF code here: http://goo.gl/9onrFN I want the client to be able to add a portfolio site link (if applicable) to the artist page and the link would say "View Artist's Website" and the link would take the user to the artist's site in a new window. How would I go about not making this text not visible, unless there was a url entered into the custom field on the post? Here's the code:
<p><?php the_field('contact_phone_number'); ?><br />
or <?php the_field('contact_email'); ?><br />
View Artist's Website</p>
Thanks in advance!
You can check if a ACF field is set with:
if(get_field('artist_website')) {
the_field('artist_website');
}
Using the_field will simple echo the contents of your field, whereas get_field will return the value which is a lot more helpful. For example you could write the above code as:
Note: get_field simple returns the value of the field, if you want to check if a valid url has been entered you will have to use a regular expression.
Below is your code with an if statement performing an empty field check:
<p>
<?php the_field('contact_phone_number'); ?><br />
or <?php the_field('contact_email'); ?>
<?php if(get_field('artist_website')) { ?>
<br />View Artist's Website
You may find your code easier to read by pre-setting your variables and including HTML in the echo:
<p>
<?php
$contact_phone_number = get_field('contact_phone_number');
$contact_email = get_field('contact_email');
$artist_website = get_field('artist_website');
echo "{$contact_phone_number}<br />";
echo "or <a href='mailto:{$contact_email}'>{$contact_email}</a><br/ >;
if($artist_website) {
echo "View <a href='{$artist_website}' target='_blank'>Artist's website</a>";
}
?>
</p>

Wordpress - let post author pick best comment (frontend)

From the front end, I want to allow the author to pick up the best comment/answer of his post. I am close, but not there yet. Every time I click the button, all comments of that post are selected as best (this is wrong). It should only select one comment per post.
In function.php I have the following:
add_action ('comment_post', 'add_comment_field', 1);
function add_comment_field($comment_id) {
add_comment_meta($comment_id, 'bestanswer', 'no', false);
}
In comment.php I have the following:
<?php
$current_author = get_the_author_meta('nickname');
global $current_user; get_currentuserinfo();
if ($current_user->nickname == $current_author) {
?>
<?php
$my_post_meta = get_comment_meta($comment->comment_ID, 'bestanswer', true);
if ( $my_post_meta == 'yes' ) {
echo '<h4>Best answer</h4>';
} else {
?>
<form method="post" action="" id="bestanswer-<?php comment_ID() ?>">
<input type="submit" name="confirm" value="Best Answer" />
</form>
<?php
if (isset($_POST['confirm'])) {
update_comment_meta($comment->comment_ID, 'bestanswer', 'yes');
}
}
?>
Thoughts? Suggestions? Any help will be appreciated.
I solved this by adding a link to each comment. The link goes to a page that displays that comment. On this page, the author then selects the comment as best answer. Now the comment meta is only saved for that particular comment. I know is an extra step, but I prefer this method since the author has to make sure that that is the comment he wants to pick. Once the comment is selected as best, author cannot change it.

Symfony - access related table field from within a form

Is it possible to get a property of a related table of a field within a form? I'm trying to get a property called "getIdentifier" which belongs to the OriginalStone model class.
<?php foreach ($form['treatedStones'] as $fields):?>
<?php $fields['original_stone_id']->renderRow() ?>
<?php endforeach?>
I was hoping I could do something like:
<?php foreach ($form['treatedStones'] as $fields):?>
<?php $fields['original_stone_id']->getObject()->getIdentifier() ?>
<?php endforeach?>
Thanks
I assume treatedStones is an embedded doctrine form. Try $form->getEmbeddedForm('treatedStones')->getObject()->getOriginalStone()->getIdentifier()

Resources