Adding code to insert user-entered code from Admin area
-
I’d like to add code into my template that will insert user-entered HTML through a settings option on the “Edit Post” page. How complex is this task? My assumption would be to add and ID a field on the admin page, and then input code to insert that field into the template in my specified spot on the template page. Is this about right or is there a lot more to it than that?
-
This sounds like you would use a ‘custom field’.
Option 1:
Is to give instructions to the user on creating a ‘Custom Field’, lets say this custom field is to be called ‘user-html’, in this ‘WordPress 3’ un-tested example the div and style is optional.1. Create a file called user-html.php and add this code:
<?php if(get_post_meta( $post->ID, 'user-html', true )) : ?> <div class="user-html"> <?php echo stripslashes((get_post_meta( $post->ID, 'user-html', true )); ?> </div> <?php endif; ?>2. Add any styles for the ‘user-html’ div in the style.css
.user-html{ display:block; color:#999; width:150px; height:200px; overflow:hidden; }3. In the template page add the code where you want the html:
<?php /* Custom Code Get User HTML */ ?> <?php get_template_part('user','html'); ?>That’s it!
Option 2
If you want ‘Control’ of the field, and for it to be there as default then you need a bit of code, have a look at this post by Steve TaylorHTH
David
David that was exactly what I needed, THANK YOU!
Sorry another question… where should the php file be located? It doesn’t appear that any of this code references this file. How does the template call this file?
In ‘WordPress 3’ there is a new way of reading the files, get_template_part() lets say we needed two navigation bars, we would add the different code in two files, navigation-1.php and navigation-2.php.
We would call these as:
<?php get_template_part('navigation',1); ?> <?php get_template_part('navigation',2); ?>So the file user-html.php will go into the ‘theme’ or ‘child theme’ folder and is called with the function:
<?php get_template_part('user','html'); ?>I use these lots of times in my twenty ten tutorials
This way of working is tidy when adding blocks of code, limits the include_once(‘filename.php’) errors, so they are a ‘win win’
HTHDavid
That’s awesome! Okay, makes sense so I’ll give it a shot and see how it goes. Thanks again!
So I know I’m really close, but somehow it’s not quite working yet. Here’s what I have, based on info from the link you provided.
In my functions file:
/** * Define custom field for ECWID HTML content */ if ( !class_exists('myCustomFields') ) { class myCustomFields { /** * @var string $prefix The prefix for storing custom fields in the postmeta table */ var $prefix = '_mcf_'; /** * @var array $customFields Defines the custom fields available */ var $customFields = array( array( "name" => "ecwid-content", "title" => "ECWID HTML content", "description" => "", "type" => "textarea", "scope" => array( "post" ), "capability" => "edit_posts" ) ); /** * PHP 4 Compatible Constructor */ function myCustomFields() { $this->__construct(); } /** * PHP 5 Constructor */ function __construct() { add_action( 'admin_menu', array( &$this, 'createCustomFields' ) ); add_action( 'save_post', array( &$this, 'saveCustomFields' ), 1, 2 ); // Comment this line out if you want to keep default custom fields meta box add_action( 'do_meta_boxes', array( &$this, 'removeDefaultCustomFields' ), 10, 3 ); } /** * Remove the default Custom Fields meta box */ function removeDefaultCustomFields( $type, $context, $post ) { foreach ( array( 'normal', 'advanced', 'side' ) as $context ) { remove_meta_box( 'postcustom', 'post', $context ); remove_meta_box( 'postcustom', 'page', $context ); //Use the line below instead of the line above for WP versions older than 2.9.1 //remove_meta_box( 'pagecustomdiv', 'page', $context ); } } /** * Create the new Custom Fields meta box */ function createCustomFields() { if ( function_exists( 'add_meta_box' ) ) { add_meta_box( 'my-custom-fields', 'Custom Fields', array( &$this, 'displayCustomFields' ), 'page', 'normal', 'high' ); add_meta_box( 'my-custom-fields', 'Custom Fields', array( &$this, 'displayCustomFields' ), 'post', 'normal', 'high' ); } } /** * Display the new Custom Fields meta box */ function displayCustomFields() { global $post; ?> <div class="form-wrap"> <?php wp_nonce_field( 'my-custom-fields', 'my-custom-fields_wpnonce', false, true ); foreach ( $this->customFields as $customField ) { // Check scope $scope = $customField[ 'scope' ]; $output = false; foreach ( $scope as $scopeItem ) { switch ( $scopeItem ) { case "post": { // Output on any post screen if ( basename( $_SERVER['SCRIPT_FILENAME'] )=="post-new.php" || $post->post_type=="post" ) $output = true; break; } case "page": { // Output on any page screen if ( basename( $_SERVER['SCRIPT_FILENAME'] )=="page-new.php" || $post->post_type=="page" ) $output = true; break; } } if ( $output ) break; } // Check capability if ( !current_user_can( $customField['capability'], $post->ID ) ) $output = false; // Output if allowed if ( $output ) { ?> <div class="form-field form-required"> <?php switch ( $customField[ 'type' ] ) { case "checkbox": { // Checkbox echo '<label for="' . $this->prefix . $customField[ 'name' ] .'" style="display:inline;"><b>' . $customField[ 'title' ] . '</b></label> '; echo '<input type="checkbox" name="' . $this->prefix . $customField['name'] . '" id="' . $this->prefix . $customField['name'] . '" value="yes"'; if ( get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) == "yes" ) echo ' checked="checked"'; echo '" style="width: auto;" />'; break; } case "textarea": case "wysiwyg": { // Text area echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><b>' . $customField[ 'title' ] . '</b></label>'; echo '<textarea name="' . $this->prefix . $customField[ 'name' ] . '" id="' . $this->prefix . $customField[ 'name' ] . '" columns="30" rows="3">' . htmlspecialchars( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '</textarea>'; // WYSIWYG if ( $customField[ 'type' ] == "wysiwyg" ) { ?> <script type="text/javascript"> jQuery( document ).ready( function() { jQuery( "<?php echo $this->prefix . $customField[ 'name' ]; ?>" ).addClass( "mceEditor" ); if ( typeof( tinyMCE ) == "object" && typeof( tinyMCE.execCommand ) == "function" ) { tinyMCE.execCommand( "mceAddControl", false, "<?php echo $this->prefix . $customField[ 'name' ]; ?>" ); } }); </script> <?php } break; } default: { // Plain text field echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><b>' . $customField[ 'title' ] . '</b></label>'; echo '<input type="text" name="' . $this->prefix . $customField[ 'name' ] . '" id="' . $this->prefix . $customField[ 'name' ] . '" value="' . htmlspecialchars( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '" />'; break; } } ?> <?php if ( $customField[ 'description' ] ) echo '<p>' . $customField[ 'description' ] . '</p>'; ?> </div> <?php } } ?> </div> <?php } /** * Save the new Custom Fields values */ function saveCustomFields( $post_id, $post ) { if ( !wp_verify_nonce( $_POST[ 'my-custom-fields_wpnonce' ], 'my-custom-fields' ) ) return; if ( !current_user_can( 'edit_post', $post_id ) ) return; if ( $post->post_type != 'page' && $post->post_type != 'post' ) return; foreach ( $this->customFields as $customField ) { if ( current_user_can( $customField['capability'], $post_id ) ) { if ( isset( $_POST[ $this->prefix . $customField['name'] ] ) && trim( $_POST[ $this->prefix . $customField['name'] ] ) ) { $value = $_POST[ $this->prefix . $customField['name'] ]; // Auto-paragraphs for any WYSIWYG if ( $customField['type'] == "wysiwyg" ) $value = wpautop( $value ); update_post_meta( $post_id, $this->prefix . $customField[ 'name' ], $value ); } else { delete_post_meta( $post_id, $this->prefix . $customField[ 'name' ] ); } } } } } // End Class } // End if class exists statement // Instantiate the class if ( class_exists('myCustomFields') ) { $myCustomFields_var = new myCustomFields(); }And, in my template:
<?php if ($ecwid_content <> '') { ?> <?php echo($ecwid_content); ?> <?php }; ?>Have I messed it up too badly?
<?php echo get_post_meta( $post->ID, '_mcf_ecwid_content', true); ?>HTH
David
The topic ‘Adding code to insert user-entered code from Admin area’ is closed to new replies.