May 28, 2013 at 6:27 pm 4285 |
May 29, 2013 at 11:17 am 4307 |
noomia
| Hi Tarek !
Yes I am. In fact I have the field in the default custom field box of WordPress, but I want it in the WPUF box…
What I do :
In my WPUF form, I have an action hook field with this action hook ‘which_companies_hook’.
If we are in a front-end page, I generate a hidden field which contains the company ID.
If we are in the admin, the hidden field is replaced by a select dropdown which contains all the company’s name (related to the right ID), so I can change the company as an admin.
Here is my code :
function hidden_field_which_company( $form_id, $post_id, $form_settings ) {
global $current_user;
$post = get_post($post_id);
if(get_post_meta( $post_id,'company_id', true )) {
$postParent = get_post_meta( $post_id,'company_id', true );
}
if (is_admin()) { //If we are in the wp-admin, dropdown list
// generate your post dropdown
$args= array(
'post_type' => 'companies',
'showposts' => -1,
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'title'
);
$my_companies_query = new WP_Query($args);
?>
<div class="wpuf-fields">
<select id="whichCompany" name="whichCompany" data-type="select">
<option value="">None</option>
<?php while ($my_companies_query->have_posts()) : $my_companies_query->the_post(); ?>
<option value="<?php echo (get_the_ID()); ?>" <?php if(get_the_ID() == $postParent) { ?>selected="selected" <?php } ?>><?php the_title(); ?></option>
<?php endwhile;?>
</select>
<span class="wpuf-help"></span>
</div>
<?php
wp_reset_postdata();
} else { //If we are not in the front-end, hidden field
if($_GET['companyId']) { //On est sur une page de création
$post = get_post($_GET['companyId']);
if ( $current_user->ID == $post->post_author ){ ?>
<div class="wpuf-fields">
<input type="hidden" id="whichCompany" name="whichCompany" value="<?php echo($_GET['companyId']); ?>" />
</div>
<?php } else {
header('Location: '.home_url());
exit();
}
} else {
if($postParent) { //On est sur une page d'édition
?>
<div class="wpuf-fields">
<input type="hidden" id="whichCompany" name="whichCompany" value="<?php echo($postParent); ?>" />
</div>
<?php
} else {
header('Location: '.home_url());
exit();
}
}
}
}
add_action( 'which_companies_hook', 'hidden_field_which_company', 10, 3 );
function wpufe_insert_postParent( $post_id ) {
if ( isset( $_POST['whichCompany'] ) ) {
$my_post = array();
$my_post['ID'] = $post_id;
$newpost_id=wp_update_post( $my_post );
if($newpost_id!=0) {
$whichCompany=$_POST['whichCompany'];
update_post_meta ($newpost_id, 'company_id', $whichCompany);
}
}
}
add_action( 'wpuf_add_post_after_insert', 'wpufe_insert_postParent' );
add_action( 'wpuf_edit_post_after_update', 'wpufe_insert_postParent' );
I have the hidden field in the Front-end, but I have nothing (hidden or dropdown) in the wp-admin (in the WPUF custom field box).
How can I have the dropdown in the admin ?
Thanks !
|
June 3, 2013 at 7:43 am 4458 |
June 3, 2013 at 4:50 pm 4474 |
Tareq Hasan
| Oh, now I get it. In the admin panel, only custom fields are displayed in the “WPUF Custom Fields” meta box, you can’t see the action hooks. In that case, you’ve add your own meta box.
|
June 4, 2013 at 2:11 pm 4534 |
June 4, 2013 at 3:13 pm 4538 |
June 4, 2013 at 3:26 pm 4541 |