I created a small plugin where users can set multiple featured image by clicking Add New
link from the meta box and remove them as well. The user can use Set featured image
link to select featured image from media library. The Add New
link clones (using javascript) previous meta box (with appropriate filters) to create a new meta box whereas the remove button removes the meta box.
Problem and Questions
-
I am currently using only single nonce field for every meta box generated. Many previous threads suggest that new nonce field should be added for every meta box. How can i create different nonce field if the box is being cloned using javascript? Should i use AJAX? or is it even necessary to use nonce field in this case as user can only select image from media library?
-
The hidden input field in meta box is used as an array (see the html for meta box) and is saved using
update_post_meta
. If the nonce field is added for every dynamically added meta box how can i check it while saving the post?
if ( !wp_verify_nonce( noncefields..., plugin_basename(__FILE__) ) ) {
return;
}
The html for the meta box looks like this.
<?php wp_nonce_field( plugin_basename(__FILE__), 'dfi_fimageplug'); //this is generated only once for all meta box ?>
<a href="https://wordpress.stackexchange.com/questions/115192/javascript:void(0)" class="dfiFeaturedImage"><?php _e('Set featured image', 'ap_dfi_dynamic-featured-image') ?></a><br/>
<img src="<?php if( !empty($featuredImgTrimmed) ) echo site_url() . $featuredImgTrimmed ?>" class="dfiImg <?php if( is_null($featuredImgTrimmed) ) echo "dfiImgEmpty' ?>'/>
<div class="dfiLinks">
<a href="https://wordpress.stackexchange.com/questions/115192/javascript:void(0)" data-id='<?php echo $featuredId ?>' class="dfiAddNew"><?php _e('Add New', 'ap_dfi_dynamic-featured-image') ?></a>
<a href="https://wordpress.stackexchange.com/questions/115192/javascript:void(0)" class="dfiRemove"><?php _e('Remove', 'ap_dfi_dynamic-featured-image') ?></a>
</div>
<div class="dfiClearFloat"></div>
<input type="hidden" name="dfiFeatured[]" value="<?php echo $featuredImg ?>" />
I am not very experienced in plugin development and am really confused on this matter. Please suggest.