Solved

Adjustment in small Wordpress plugin?

Posted on 2011-09-25
10
284 Views
Last Modified: 2012-05-12
This is a small wordpress widget called dashboard-post-it.

It's a text widget that lets me, as the admin, post a note on the dashboard for all users to see.

Is it possible to adjust the code so that the text widget appears on the 'Add New Post' page in wp-admin - under the main text box where the body of the post is entered, instead of on the dashboard?

If so, can you show me how? The goal is to provide instructions/tips to writers right on that page where they are writing.

Thanks,  Chris
<?php

/*
Plugin Name: Dashboard Post-it
Plugin URI: http://wordpress.org/extend/plugins/dashboard-post-it/
Description: Adds a simple 'Post-it' widget to the Dashboard that allows you to leave yourself or other authors a note. HTML allowed, HTML restrictions observed. Based on code from <a href="http://wpengineer.com/add-wordpress-dashboard-widgets/">WP Engineer</a> and <a href="http://rick.jinlabs.com/2009/02/01/how-add-options-to-your-wordpress-27-dashboard-widgets/">Ricardo Gonz&aacute;lez Castro</a>.
Version: 1.1
Author: Mark Dingemanse
Tags: plugin, dashboard, widget, note
Requires at least: 2.7
Tested up to: 2.8.4
Author URI: http://ideophone.org/
*/

/*
 * Spit out the content of the Post-it Widget
 */

function dashboard_postit() {
        $widget_options = dashboard_postit_Options();
		// echo "{$widget_options['pi_text']}";
		print wpautop($widget_options['pi_text']);

}
 
 
/**
 * Manage the defaults and the options
 */
function dashboard_postit_Options() {

	$defaults = array( 'pi_title' => 'Post-it', 'pi_text' => 'Your text here');
	if ( ( !$options = get_option( 'dashboard_postit' ) ) || !is_array($options) )
		$options = array();
	return array_merge( $defaults, $options );
	global $defaults;
}

/**
 * Add the Post-it Widget via function wp_add_dashboard_widget()
 */
 
function dashboard_postit_Init() {
	$options = dashboard_postit_Options();
	$title = $options['pi_title'];
	wp_add_dashboard_widget( 'dashboard_postit', $title, 'dashboard_postit', 'dashboard_postit_Setup' );
}

/**
 * Edit the options
 */

function dashboard_postit_Setup() {
 
	$options = dashboard_postit_Options();
 
		// Get our options and see if we're handling a form submission.
	if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) && isset( $_POST['widget_id'] ) && 'dashboard_postit' == $_POST['widget_id'] ) {
		foreach ( array( 'pi_title', 'pi_text' ) as $key )
				$options[$key] = stripslashes($_POST[$key]);
				if ( !current_user_can('unfiltered_html') )
					$newoptions['text'] = stripslashes(wp_filter_post_kses($newoptions['text'])); // This should take care of HTML permissions.
		update_option( 'dashboard_postit', $options );
	}
		
		// Format the options to be valid HTML attributes.
		// $title = htmlspecialchars($options['pi_text'], ENT_QUOTES);
		
?>
		<label style="display:block;margin-bottom:4px;" for="pi_title">Widget title: <input type="text" id="pi_title" name="pi_title" value="<?php echo $options['pi_title'] ?>" /></label>
		<label style="display:block;margin-bottom:4px;" for="pi_text">Your note: <textarea style="width: 99%; height: 200px;margin:4px 0;" id="pi_text" name="pi_text"><?php echo $options['pi_text'] ?></textarea></label>
<?php
}
 
/**
 * Use the hook to integrate our new widget
 */
add_action('wp_dashboard_setup', 'dashboard_postit_Init');
 
?>

Open in new window

0
Comment
Question by:St_Aug_Beach_Bum
  • 6
  • 3
10 Comments
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 36597088
If you want to add some text to the text editor, you can use this trick to place instructions directly into the post/page editor:

<?php
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
  $content = "<h2>Title of the Help Section Here</h2><br /><p>Add some explanation or instructions here. It will automatically be placed in the WordPress Editor when creating a new post.</p>";
  return $content;
  }
?>

Open in new window


That would go into the functions.php file. It's not exactly what you asked, but I thought you might be able to use this method?

Regards,
jeremyjared74
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 36597173
Hi Jeremy,

That's a neat trick - definitely a great way to put something where they will see it :)

However, I need to put the instructions below the editor, not in it, so they can reference the instructions while they write.

But thank you anyway,    Chris  
0
 
LVL 31

Expert Comment

by:gwkg
ID: 36601593
add_meta_box() is the function that will create a box under the editor.

http://codex.wordpress.org/Function_Reference/add_meta_box
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:St_Aug_Beach_Bum
ID: 36666390
Would I replace this part:

-----------
function dashboard_postit_Init() {
      $options = dashboard_postit_Options();
      $title = $options['pi_title'];
      wp_add_dashboard_widget( 'dashboard_postit', $title, 'dashboard_postit', 'dashboard_postit_Setup' );
---------

with that?
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 36666395
Or maybe that last line?
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 37015802
I ended up just writing it into the source code in wp-admin dir. Never got a clear answer on this, maybe too difficult.
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 37015927
I've requested that this question be closed as follows:

Accepted answer: 0 points for St_Aug_Beach_Bum's comment http:/Q_27326952.html#37015802

for the following reason:

Accepting own solution, since no clear answer and just wrote it into source.
0
 
LVL 23

Accepted Solution

by:
jeremyjared74 earned 500 total points
ID: 37015928
I actually found a way to do this. You really shouldn't hack the core files. I forgot about this question.

If you're still interested in a better way, use this:

Open your functions.php file and past this in it:
<?php 
if ( is_admin() ) {
  function add_post_enctype() {
      echo 
      "<script type='text/javascript'>
          jQuery(document).ready(function(){
          jQuery('#post').attr('enctype','multipart/form-data');
          });
        </script>";
  }
add_action('admin_head', 'add_post_enctype');
}
$new_meta_boxes =
array(
  "start" => array(
  "name" => "start",
  "type" => "start"),
  "std" => "",
  "type" => "text",
  "title" => "Tagline",
  "description" => "",
  "std" => "",
  );
function new_meta_boxes() {
  global $post, $new_meta_boxes;
  foreach($new_meta_boxes as $meta_box) {
    $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'', true);
    if($meta_box_value == "")
    $meta_box_value = $meta_box['std'];
    if($meta_box['type'] == "start") {
    echo "<div class='optionsbox'><style type='text/css'>.optionsbox {
      display:block;
      width:auto;
      float:none;
      overflow: hidden;
      }
      
    #new-meta-boxes:after {
    content:\"More Text Here!\";
    display:block;
    width:100%;
    height:10px;
    color:black;
    padding:2px 10px 20px;
    }
    </style>";
    }   
    }
    }
function create_meta_box() {
  global $theme_name;
  if ( function_exists('add_meta_box') ) {
    add_meta_box( 'new-meta-boxes', 'You Can Add Your Text Here', 'new_meta_boxes', 'post', 'normal', 'high' );
    add_meta_box( 'new-meta-boxes', 'Page Options', 'new_meta_boxes', 'page', 'normal', 'high' );

  }  
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata', 12);?>

Open in new window


It's a little bit more than you need for your bit of text, but it will be better than hacking the core.

Notice where I have the text.
add_meta_box( 'new-meta-boxes', 'You Can Add Your Text Here', 'new_meta_boxes', 'post', 'normal', 'high' );

Open in new window


Change the text   'You Can Add Your Text Here' To whatever you want it to say.

If you need to add text below it I also used the CSS in the code above to place the text below the header in this part of the code:
    
#new-meta-boxes:after {
    content:\"More Text Here!\";
    display:block;
    width:100%;
    height:10px;
    color:black;
    padding:2px 10px 20px;
    }

Open in new window


Since the CSS is inside of a PHP function, it needs the \\  \\ to escape the double quotes. It could be refined to work better, but I don't have the time.

I'll post the entire piece of code I now use to add custom image uploads to my themes you might find it useful someday?

<?php 
if ( is_admin() ) {
  function add_post_enctype() {
      echo 
      "<script type='text/javascript'>
          jQuery(document).ready(function(){
          jQuery('#post').attr('enctype','multipart/form-data');
          });
        </script>";
  }
add_action('admin_head', 'add_post_enctype');
}
$new_meta_boxes =
array(
  "start" => array(
  "name" => "start",
  "type" => "start"),
  "image" => array(
  "name" => "image",
  "std" => "",
  "type" => "image",
  "title" => "Thumbnail Image"), 
  "bannerimage" => array(
  "name" => "bannerimage",
  "std" => "",
  "type" => "image",
  "title" => "Banner Image"), 
  "tagline" => array(
  "name" => "tagline",
  "std" => "",
  "type" => "text",
  "title" => "Tagline",
  "description" => "Tagline to your post"),
  "end" => array(
  "name" => "end",
  "type" => "end"),
  );
function new_meta_boxes() {
  global $post, $new_meta_boxes;
  foreach($new_meta_boxes as $meta_box) {
    $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'', true);
    if($meta_box_value == "")
    $meta_box_value = $meta_box['std'];
    echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
    if($meta_box['type'] == "start") {
    echo "<div class='optionsbox'><style type='text/css'>.optionsbox {
      display:block;
      width:auto;
      float:none;
      overflow: hidden;
      }
    .optionsbox input, .optionsbox textarea {
      outline:none;
      padding:5px;
      color:#999;
      }
    .optionsbox input:focus, .optionsbox textarea:focus {
      border-color:#999;
      color:#666;
      }
    .optionsbox p {
      margin-bottom:20px;
      }
    .optionsbox label {
      width:140px;
      display:block;
      float:left;
      margin-top:3px;
      }
    .optionsbox small {
      padding-left:140px;
      padding-top:3px;
      color:#999;
      }
    </style>";
    } else if($meta_box['type'] == "end") { 
    	echo '</div>';
    } else if($meta_box['type'] == "image") { 
    	echo $meta_box['before'];
    	echo '<div style="background:#f4f4f4;padding:10px;height:120px;margin:0 0 20px 0;display:block">';
    	if($meta_box_value) { echo '<img style="float:right" src="'.get_bloginfo('template_directory').'/scripts/timthumb.php?src='.$meta_box_value.'&w=120&h=120" alt="" />'; }
    	echo'<p><label for="'.$meta_box['name'].'_upload">'.$meta_box['title'].'</label>';
    	echo'<input type="file" name="'.$meta_box['name'].'_upload" size="55" /><br />';
    	echo'<small>Upload image here</small></p>';
    	echo'<p><label>&nbsp;</label>';
    	echo'<input type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
    	echo'<small>or add a URL to the image here</small></p>';
    	echo '</div>';
    } else if($meta_box['type'] == "text") { 
      	echo $meta_box['before'];
      	echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
      	echo'<input style="color:#666;" type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
      	echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
    } else if($meta_box['type'] == "checkbox") { 
      	echo $meta_box['before'];
      	echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
      	if($meta_box_value) { $checked = "checked=\"checked\""; } else { $checked = ""; }
      	echo '<input style="display:block;float:left;width:20px;margin:5px 0 0 0;" '.$checked.' type="checkbox" name="'.$meta_box['name'].'" /><br/>';
      	echo'<small style="clear:both;padding-left:140px;padding-top:3px;display:block;">'.$meta_box['description'].'</small></p>';
    } else if($meta_box['type'] == "textarea") { 
      	echo $meta_box['before'];
      	echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
      	echo'<textarea style="color:#666;" name="'.$meta_box['name'].'" cols="50" rows="4">'.stripslashes($meta_box_value).'</textarea><br />';
      	echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
        } 
    }
}
function create_meta_box() {
  global $theme_name;
  if ( function_exists('add_meta_box') ) {
    add_meta_box( 'new-meta-boxes', 'Post Options', 'new_meta_boxes', 'post', 'normal', 'high' );
    add_meta_box( 'new-meta-boxes', 'Page Options', 'new_meta_boxes', 'page', 'normal', 'high' );
  }
}
function save_postdata( $post_id ) {
  global $post, $new_meta_boxes;
  $post_id = wp_is_post_revision($post_id);
  foreach($new_meta_boxes as $meta_box) {
  	if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
  	return $post_id;
  	}
  	if ( 'page' == $_POST['post_type'] ) {
      	if ( !current_user_can( 'edit_page', $post_id ))
      	return $post_id;
    	} else {
      	if ( !current_user_can( 'edit_post', $post_id ))
      	return $post_id;
      	}
    $imageuploadlocation = "";
    $metaboxname = "";
    $metaboxname_upload = "";
    if($meta_box['type'] == 'image') {
    	$metaboxname = $meta_box['name'];
    	$metaboxname_upload = $metaboxname.'_upload';
    	if($_FILES[$metaboxname_upload]['name'] != "") {
    		$overrides = array( 'test_form' => false);
    		$imagefile=wp_handle_upload($_FILES[$metaboxname_upload], $overrides);
    		$imageuploadlocation = $imagefile['url'];
    		delete_post_meta($post_id, $metaboxname, get_post_meta($post_id, $metaboxname, true));
    		add_post_meta($post_id, $metaboxname, $imageuploadlocation, true);
    	} else {
    		$imageuploadlocation = get_post_meta($post_id, $metaboxname, true);
    		delete_post_meta($post_id, $metaboxname, get_post_meta($post_id, $metaboxname, true));
    		add_post_meta($post_id, $metaboxname, $_POST[$metaboxname], true);
    	   } 
    } else {
    	$data = $_POST[$meta_box['name'].''];
    	if(get_post_meta($post_id, $meta_box['name'].'') == "")
    	add_post_meta($post_id, $meta_box['name'].'', $data, true);
    	elseif($data != get_post_meta($post_id, $meta_box['name'].'', true))
    	update_post_meta($post_id, $meta_box['name'].'', $data);
    	elseif($data == "")
    	delete_post_meta($post_id, $meta_box['name'].'', get_post_meta($post_id, $meta_box['name'].'', true));
      }
  }
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata', 12);?>

Open in new window


You'll need to modify the code above, but the first the first sample can simply be pasted into the functions.php file (after changing your text).

NOTE:
I'm only objection to get your attention. If you still wish to close it out, go ahead.
0
 

Author Closing Comment

by:St_Aug_Beach_Bum
ID: 37020164
Jeremy, thank you very much!!!

Yeh, the 'objection' got my attention - never had that happen before :)

But this is great and thanks again!

Chris
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 37020395
You're welcome.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In Part I (http://www.experts-exchange.com/Web_Development/Blogs/WordPress/A_8410-Getting-Started-In-WordPress-Part-I.html), I introduced you to the powerful WordPress backend, the WordPress administrative Dashboard.  In Part II, I will introduce yo…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question