• Hey guys i just wanted to develop a simple plugin for my personal use. I want to have an additional field in the Create New Post/Page page that inputs an url for that particular post.. Basically every differant post should have a differant url but they needn’t be unique..

    And in the single.php or page.php I want to display this url.. As simple as that.. But I am not used to coding plugins for wordpress so I have just asked you guys..

    Please help…

Viewing 15 replies - 1 through 15 (of 18 total)
  • Redirectify perhaps?

    http://www.redalt.com/downloads/

    Thread Starter coolkarthik88

    (@coolkarthik88)

    well it is something similar i guess. Well thanks for the plugin…

    Thread Starter coolkarthik88

    (@coolkarthik88)

    no this is not what i want..

    i want a plugin that inputs a url from the create post page and displays that url on the Post itself(single)..

    bascially i want the url to be in wp-posts table as a column.

    Why not just use a custom field? Then you can display it with Scott Reilly’s get custom field plugin.

    I don’t know why just using custom field isn’t good enough.

    However, if for any reason you REALLY need it in the wp_posts table, try this.

    posturl.php ==================================

    <?php
    /*
    Plugin Name: posturl
    Plugin URI:
    Description:
    Author: Yoshi
    Version: 0.5b
    */

    add_action('wp_insert_post', 'posturl_insert_post');
    function posturl_insert_post($pID) {
    global $wpdb;

    extract($_POST);

    $wpdb->query(
    "UPDATE $wpdb->posts SET
    post_url = '$post_url'
    WHERE ID = $pID");

    }

    add_action('dbx_post_sidebar', 'posturl_addfield');

    function posturl_addfield() {
    global $post;
    ?>
    <fieldset id="posturl" class="dbx-box">
    <h3 class="dbx-handle"><?php _e('Post URL'); ?>:</h3>
    <div class="dbx-content"><input name="post_url" type="text" size="13" id="post_url" value="<?php echo $post->post_url ?>" /></div>
    </fieldset>

    <?php
    }

    add_action('activate_posturl.php', 'posturl_addcolumn');

    function posturl_addcolumn () {
    global $wpdb;
    $wpdb->query("ALTER TABLE $wpdb->posts ADD COLUMN post_url varchar(128)");
    }
    ?>

    =============================================
    And post_url can be called like <?php echo $post->url ?> in your template.

    Should have url validation and display function but don’t have time right now…

    Thread Starter coolkarthik88

    (@coolkarthik88)

    oh thanks for the help I’ll see if this helps me..

    Thread Starter coolkarthik88

    (@coolkarthik88)

    Well I modified which yoshi gave me but it dosen’t work perfectly..

    I have two problems :
    1) The URL is not added to the database
    2) How on earth am i supposed to show the url on post (please write the display function..)

    Well here is my code..


    <?php
    /*
    Plugin Name: Digg This
    Plugin URI: http://xyz.com
    Description: Adds the Digg URL to your site..
    Author: Karthik
    Version: 1.0
    */

    add_action('wp_digg_this', 'diggurl_insert_post');
    function diggurl_insert_post($pID) {
    global $wpdb;

    extract($_POST);

    $wpdb->query(
    "UPDATE $wpdb->posts SET
    digg_url = '$digg_url'
    WHERE ID = $pID");

    }

    add_action('dbx_post_sidebar', 'diggurl_addfield');

    function diggurl_addfield() {
    global $post;
    ?>
    <fieldset id="posturl" class="dbx-box">
    <h3 class="dbx-handle"><?php _e('Enter the Digg URL'); ?>:</h3>
    <div class="dbx-content"><input name="digg_url" type="text" size="21" id="digg_url" value="<?php echo $post->digg_url ?>" /></div>
    </fieldset>

    <?php
    }

    add_action('activate_diggthis.php', 'diggurl_addcolumn');

    function diggurl_addcolumn () {
    global $wpdb;
    $wpdb->query("ALTER TABLE $wpdb->posts ADD COLUMN digg_url varchar(128)");
    }

    add_action('deactivate_diggthis.php', 'diggurl_removecolumn');

    function diggurl_removecolumn () {
    global $wpdb;
    $wpdb->query("ALTER TABLE $wpdb->posts DROP digg_url“);
    }

    function show_digg_this () {

    }
    ?>

    Please help me out..

    1) add_action('wp_digg_this', 'diggurl_insert_post');
    this should be
    add_action('wp_insert_post', 'diggurl_insert_post');

    I have not read through your code carefully so there might be somewhere else to fix. If it still doesn’t work please try my original code and let me know if it works.

    2) The simplest one would be something like:

    function show_digg_this () {
    $post = &get_post();
    $diggurl = $post->digg_url();
    if ( strlen($diggurl) > 0 )
    echo '<a href="' . $diggurl . '">' . $diggurl . '</a>';
    }

    And you can call <?php show_digg_this (); ?> in your template.

    Again, don’t forget to add some validation at submission and display(especially if you allow users to post).

    Regards,

    // can anyone let me know how to indent within the code?

    Thread Starter coolkarthik88

    (@coolkarthik88)

    Well I have got most of it working..

    But there are some problems. The Digg url still dosen’t show up eventhough it is there in the database..

    And how do i get the permalink of the post in the code (see below)

    add_action('wp_insert_post', 'diggurl_insert_post');
    function diggurl_insert_post($pID) {
    global $wpdb;

    extract($_POST);

    $wpdb->query(
    "UPDATE $wpdb->posts SET
    digg_url = '$digg_url'
    WHERE ID = $pID");

    }

    add_action('dbx_post_sidebar', 'diggurl_addfield');

    function diggurl_addfield() {
    global $post;
    ?>
    <fieldset id="posturl" class="dbx-box">
    <h3 class="dbx-handle"><?php _e('Enter the Digg URL'); ?>:</h3>
    <div class="dbx-content"><input name="digg_url" type="text" size="21" id="digg_url" value="<?php echo $post->digg_url ?>" /></div>
    </fieldset>

    <?php
    }

    add_action('activate_diggthis.php', 'diggurl_addcolumn');

    function diggurl_addcolumn () {
    global $wpdb;
    $wpdb->query("ALTER TABLE $wpdb->posts ADD COLUMN digg_url varchar(128)");
    }

    add_action('deactivate_diggthis.php', 'diggurl_removecolumn');

    function diggurl_removecolumn () {
    global $wpdb;
    $wpdb->query("ALTER TABLE $wpdb->posts DROP digg_url“);
    }

    function show_digg_this () {
    $diggurl = $post->digg_url;
    $digg_permalink = the_permalink();

    if ( strlen($diggurl) > 0 )
    echo ‘
    $digg_url

    ;

    else
    echo ‘
    <div id=”digg_this_button”>
    <*a href=”http://digg.com/submit?phase=2&url=$digg_permalink&#8221; title=”Add to Digg”>
    <img src=”digg_button.gif” alt=”Digg Button” />
    </div>
    ‘;
    }

    Oops. my show_digg_this() had error didn’t it. Sorry I wasn’t actually testing my code.

    This time I checked this working.


    function show_digg_this ($id = 0) {
    global $post; // changed.
    $diggurl = $post->digg_url;

    if ( strlen($diggurl) > 0 )
    echo $diggurl;
    else {

    /* the_permalink() echos the url. use get_permalink() to get as valiable. */
    $digg_permalink = get_permalink($post->ID);

    /* you can't use valiables within single quotes */
    echo '<div id="digg_this_button"><a href="http://digg.com/submit?phase=2&url=&#39; . $digg_permalink . '" title="Add to Digg"><img src="digg_button.gif" alt="Digg Button" /></a>
    </div>';
    }
    }

    I’m having a hard time getting the add column to work. I really need to figure it out too! You link to two external php files in the add_action() function. How does that work? I don’t have an activate / deactivate file. I’m a little confused.

    Yoshi, your comments are awesome! hopefully you can help me out. I’m really close. I just need to drop in an additional column like coolkarthik88. Maybe I don’t fully understand the add action function. I’ll go back and do some more reading.

    yea, i just don’t understand why you have activate_diggthis.php? What is that? Why is that there. the API documentation says it needs to be a ‘hook’. Please help. Thanks.

    Hi ehmcgregor,

    ‘activate_diggthis.php’ is not a external file but the ‘activate_’ hook, which is called when plugins are activated.

    If your plugin file is myplugin.php, you can add action like:

    add_action('activate_myplugin.php','myplugin_install');

    then the function myplugin_install() will be executed on plugin activation. You can create column here.

    And as you can guess, ‘disactivate_’ hook is for disactivation. You can remove some options or custom database table if needed.

    Here is a list of wp hooks.
    http://wphooks.flatearth.org/

    Thread Starter coolkarthik88

    (@coolkarthik88)

    hey yoshi.. how do I add an image to the HTML that the plugin generates. I want the image to be in the plugin folder…

    <img src="<?php echo get_settings('siteurl') ?>/wp-content/plugins/myplugin/img.png" />

    Something like this?

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Help Me Develop a Plugin’ is closed to new replies.