WordPress.org

Ready to get started?Download WordPress

Forums

Bannerspace Slideshow
New feature: UI for the "link" post meta (1 post)

  1. BramNL
    Member
    Posted 8 months ago #

    Hi Dean Oakley,

    I have created an new functionality that you might like. I needed an user interface to add the link post meta for my users. This is what I came up with. View a screenshot (Dutch!).
    The text says:

    The following URL makes the full slide clickable.
    Enter the URL manually or select a page below. Leave the field empty to disable the link.
    Selecting a page will overwrite the manually entered URL when post is updated.

    Users can type the URL themselves, or select a page from the drop down.

    Instruction:
    At the very end of "bannerspace.php" add:

    // If in administrator back-end, load the post meta box
    if (is_admin()) {
    	require_once('bm_admin_meta_box.php');
    }

    Create a new file, named "bm_admin_meta_box.php" and paste the following code:

    <?php
    
    /* Define the custom box */
    add_action( 'add_meta_boxes', 'bm_bs_add_post_meta_box' );
    
    /* Do something with the data entered */
    add_action( 'save_post', 'bm_bs_post_meta_box_save' );
    
    /* Adds a box to the main column on the Post and Page edit screens */
    function bm_bs_add_post_meta_box() {
    	add_meta_box(
    		'bm_bs_add_post_meta_box_id',
    		__('Bannerspace full slide hyperlink'),
    		'bm_bs_post_meta_box_html',
    		'bannerspace_post'
    	);
    }
    
    /* Prints the box content */
    function bm_bs_post_meta_box_html( $post ) {
    
    	// Use nonce for verification
    	wp_nonce_field( plugin_basename( __FILE__ ), 'bm_bs_post_meta_nonce' );
    
    	// The actual fields for data entry
    	// Use get_post_meta to retrieve an existing value from the database and use the value for the form
    	$value = get_post_meta( $post->ID, 'link', true );
    	?>
    	<p>
    		<label for="bm_bs_post_meta_box_link">
    		<?php _e('The following URL makes the full slide clickable.<br />Enter the URL manually or select a page below. Leave the field empty to disable the link.'); ?>
    		</label><br />
    		<input type="text" id="bm_bs_post_meta_box_link" name="bm_bs_post_meta_box_link" value="<?php echo esc_attr($value) ?>" style="width:100%" />
    	</p>
    
    	<p>
    		<label for="bm_bs_post_meta_box_page">
    		<?php _e('Selecting a page will overwrite the manually entered URL when post is updated.', 'bannerspace'); ?>
    		</label><br />
    		<?php 
    
    			// Prepare the arguments
    			$args = array (
    				'name' => 'bm_bs_post_meta_box_page',
    				'show_option_none' => __('Select a page', 'bannerspace'),
    				'option_none_value' => '0',
    			);
    
    			// Get the pages from the content site
    			wp_dropdown_pages( $args );
    
    		?>
    	</p>
    	<?php
    }
    
    /* When the post is saved, saves our custom data */
    function bm_bs_post_meta_box_save( $post_id ) {
    
    	// First we need to check if the current user is authorised to do this action.
    	if ( 'bannerspace_post' != $_REQUEST['post_type'] ) {
    		// Only edit post_type "bannerspace_post"
    		return;
    	} else {
    		if ( ! current_user_can( 'edit_post', $post_id ) )
    			return;
    	}
    
    	// Secondly we need to check if the user intended to change this value.
    	if ( ! isset( $_POST['bm_bs_post_meta_nonce'] ) || ! wp_verify_nonce( $_POST['bm_bs_post_meta_nonce'], plugin_basename( __FILE__ ) ) )
    	return;
    
    	// Thirdly we can save the value to the database
    
    	//if saving in a custom table, get post_ID
    	$post_ID = $_POST['post_ID'];
    
    	//sanitize user input
    	$post_meta_page_id = (int)$_POST['bm_bs_post_meta_box_page'];
    
    	if ($post_meta_page_id > 0) {
    		$post_meta_link = get_permalink( $post_meta_page_id );
    
    		// Replace to real domain, if needed
    		// Used with WP Multi-site and WordPress MU Domain Mapping plugin.
    		if (function_exists('domain_mapping_post_content'))	{
    			$post_meta_link = domain_mapping_post_content($post_meta_link);
    		}
    	} else {
    		$post_meta_link = sanitize_text_field( $_POST['bm_bs_post_meta_box_link'] );
    	}
    
    	// Update post meta
    	add_post_meta($post_ID, 'link', $post_meta_link, true) or
    	update_post_meta($post_ID, 'link', $post_meta_link);
    }

    http://wordpress.org/plugins/bannerspace/

Reply

You must log in to post.

About this Plugin

About this Topic