Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

After searching several hours, I've decided to crowd-source this one. I have a client who uses Wordpress very frequently, they often have to create a link to a certain page for collecting donations - the issue is they always get the link wrong (long story, but they always screw it up!). I've been searching for a plugin or tutorial on how I can add a button to the editor that would automatically add the link to their post - ensuring the link is always correct. As mentioned I searched several hours and was wondering if someone could point me in the right direction. Thanks!

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

You can make a button to the Tinymce editor that adds a pre defined link like this youtube button:

enter image description here

Put this in your functions.php theme file:

// Hook into WordPress
add_action('init', 'mylink_button');

// Create Our Initialization Function
function mylink_button() {

   if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
     return;
   }

   if ( get_user_option('rich_editing') == 'true' ) {
     add_filter( 'mce_external_plugins', 'add_plugin' );
     add_filter( 'mce_buttons', 'register_button' );
   }

}

// Register Our Button
function register_button( $buttons ) {
    array_push( $buttons, "|", "mylink" );
    return $buttons;
}

// Register Our TinyMCE Plugin
function add_plugin( $plugin_array ) {
    $plugin_array['mylink'] = get_bloginfo( 'template_url' ) . '/link.js';
    return $plugin_array;
}

And then make a link.js file and put it in your theme root with this in it:

// JavaScript Document
(function() {
    tinymce.create('tinymce.plugins.mylink', {
        init : function(ed, url) {
            ed.addButton('mylink', {
                title : 'My Link',
                image : url+'/mylink.png',
                onclick : function() {
                     ed.selection.setContent('http://mylink.com');

                }
            });
        },
        createControl : function(n, cm) {
            return null;
        },
    });
    tinymce.PluginManager.add('mylink', tinymce.plugins.mylink);
})();

Change http://mylink.com to the link you want to add. Also add a icon called mylink.png in the theme root.

share|improve this answer
    
Pontius - Excellent post and example code. This is exactly what I was searching for! Thank you! Cheers! –  BD_Design Jul 2 '12 at 15:07
add comment

I don't know what dbx_post_advanced hook is for, but you might be able to use it to do what you want.

static function insert_some_text( $arg1 ) {
   global $post ;

   if( $post->post_name == '' ) {
       $post->post_content = 'Some Hello World' ;
   }
}
add_action('dbx_post_advanced', 'insert_some_text');

Add this code into your functions.php and modify it. The post_name check you may want to replace with checking the URL for post-new.php.

share|improve this answer
    
Misunderstood the post and this doesn't answer the question - which is to add a button to the editor. Or the original question didn't mention adding a button? –  KDM Jun 29 '12 at 12:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.