WordPress.org

Ready to get started?Download WordPress

Forums

Help Me Develop a Plugin (19 posts)

  1. coolkarthik88
    Member
    Posted 8 years ago #

    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...

  2. schulte
    Member
    Posted 8 years ago #

    Redirectify perhaps?

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

  3. coolkarthik88
    Member
    Posted 8 years ago #

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

  4. coolkarthik88
    Member
    Posted 8 years ago #

    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.

  5. yami
    Member
    Posted 8 years ago #

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

  6. yoshi
    Member
    Posted 8 years ago #

    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...

  7. coolkarthik88
    Member
    Posted 8 years ago #

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

  8. coolkarthik88
    Member
    Posted 8 years ago #

    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..

  9. yoshi
    Member
    Posted 8 years ago #

    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?

  10. coolkarthik88
    Member
    Posted 8 years ago #

    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" title="Add to Digg">
    <img src="digg_button.gif" alt="Digg Button" />
    </div>
    ';
    }

  11. yoshi
    Member
    Posted 8 years ago #

    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=' . $digg_permalink . '" title="Add to Digg"><img src="digg_button.gif" alt="Digg Button" /></a>
    </div>';
    }
    }

  12. ehmcgregor
    Member
    Posted 8 years ago #

    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.

  13. ehmcgregor
    Member
    Posted 8 years ago #

    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.

  14. yoshi
    Member
    Posted 7 years ago #

    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/

  15. coolkarthik88
    Member
    Posted 7 years ago #

    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...

  16. yoshi
    Member
    Posted 7 years ago #

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

    Something like this?

  17. coolkarthik88
    Member
    Posted 7 years ago #

    well everything is ok for now. I moved the plugin to its own folder and after activating it doesen't create the new database column. I think there is something wrong actvate_digg_this.php hook...

  18. yoshi
    Member
    Posted 7 years ago #

    Is your plugin file digg_this.php?
    'activate_filename.php' in the hook must meets the actual file name.

    Try echo something in the function to see if the function is really being called.

    function diggurl_addcolumn () {
    die ('hook called');
    ...

  19. Qwerios
    Member
    Posted 7 years ago #

    I've been struggling a bit with the activate hook as well. I found that changing my plugin file to all lowercase and with no underscores in the name made the activation hook work.

    So instead of myPlugin.php or my_plugin.php I had to use myplugin.php and then the add_action( "activate_myplugin.php", "install_function" ) worked.

    I couldn't find it in the documentation that this was required so I discovered it with trial and error. Granted I'm quite new at this plugin writing stuff so maybe I just missed it being mentioned somewhere.

Topic Closed

This topic has been closed to new replies.

About this Topic