• Resolved dokueki

    (@dokueki)


    Hey. I’m making a plugin for my WordPress, and I need the the_content() to also have information about the posts’s title and permalink. To do that, I attempted to add a filter/action (tried both) to the_ID and letting them save the info to a variable. Here’s the code I used for those particular parts:
    add_filter('the_ID', 'wooshare_get_title'); // I also tried add_action instead
    add_filter('the_content', 'show_wooshare_links');

    $wooshare_post_info = Array();

    function show_wooshare_links($content)
    {
    global $wpdb, $wooshare_post_info;
    $table_name = $wpdb->prefix . 'wooshare_links';
    $query = "SELECT name,url,image,text,title FROM $table_name“;
    $buttons = $wpdb->get_results($query, OBJECT);
    $insert = ”;
    foreach ($buttons as $button)
    {
    $find = Array(
    ‘”‘,
    ‘%title%’,
    ‘%url%’
    );
    $replace = Array(
    ‘%22’,
    $wooshare_post_info[‘title’],
    $wooshare_post_info[‘permalink’]
    );
    $button->url = str_replace($find, $replace ,$button->url);
    $insert .= ‘url . ‘” class=”wooshare_link” id=”wooshare_’ . $button->name . ‘” title=”‘ . $item->title . ‘”><img src=”‘ . $button->image . ‘” alt=”” class=”wooshare_icon” id=”wooshare_’ . $button->name . ‘_icon” /> ‘ . $button->text . ‘‘;
    }
    return print_r($wooshare_post_info) . $content . $insert;
    }

    function wooshare_get_title($id)
    {
    global $wpdb, $wooshare_post_info;
    $table_name = $wpdb->prefix . ‘posts’;
    $post = $wpdb->fetch_row(“SELECT post_title,guid FROM $table_name WHERE id = ‘$id'”, OBJECT);
    echo $post->post_title;
    $wooshare_post_info = Array(‘title’ => $post->post_title, ‘permalink’ => $post->guid);
    return print_r($wooshare_post_info) . $id;
    }

    But the variable stays empty. I can’t echo anything during the_ID so I can’t figure out if it ever gets the data from there or not.

The topic ‘Saving Post Title Permalink To Variable’ is closed to new replies.