Forums

[resolved] Add a Custom Field within a plugin (6 posts)

  1. Proefabonnement
    Member
    Posted 8 months ago #

    I'm using a plugin, that renders a A HREF link. But I like the plugin also to open another link at the same time, so I added.
    onClick="window.open(\'http://www.website.com\')"

    So in the .php file of the plugin it ends up like this:
    $title = '<a onClick="window.open(\'http://www.website.com\')" href="#'.$id.'">'.$title.'</a>';

    But here's my problem. I'd like the link in every post to be different, by using a custom field. The custom field is called LINK. And the value is: http://www.website.com.

    Normally, I know how to add a custom field, by doing this:
    <?php echo get_post_meta($post->ID, "LINK", true);?>

    But if I'm adding the following, i'm getting an error message:
    $title = '<a onClick="window.open(\'<?php echo get_post_meta($post->ID, "LINK", true);?>\')" href="#'.$id.'">'.$title.'</a>';

    Anyone know how to solve this?

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 8 months ago #

    $title = '<a onClick="window.open(\' . echo get_post_meta($post->ID, "LINK", true); . \')" href="#'.$id.'">'.$title.'</a>';

    Not sure I got it quite right here.... but you are already inside php code... so you can't use opening and closing php tags, when php i already open

  3. Proefabonnement
    Member
    Posted 8 months ago #

    I gave it a try, but it doesn't work unfortunately.
    Looking in the HTML source of the single post, the code doesn't transform into the link. It stays exactly the same. So in the HTML code it says:
    <a onClick="window.open(' . echo get_post_meta($post->ID, "LINK", true); . ')" .....

    Is it because it's not in the loop?

  4. Big Bagel
    Member
    Posted 8 months ago #

    $title = '<a onClick="window.open(\' . echo get_post_meta($post->ID, "LINK", true); . \')" href="#' . $id . '">' . $title . '</a>';

    Remove the echo and the semicolon around get_post_meta(). Might also want to do something about the quotes within window.open().

    $title = '<a onClick="window.open(\'' . get_post_meta( $post->ID, "LINK", true ) . '\')" href="#' . $id . '">' . $title . '</a>';

    A nicer looking way of doing this might be:

    $link = get_post_meta( $post->ID, "LINK", true );
    $onclick = "window.open( '$link' )";
    $title = '<a onClick="' . $onclick . '" href="#' . $id . '">' . $title . '</a>';
  5. Proefabonnement
    Member
    Posted 7 months ago #

    Still no luck. It does open a new browser tab, like I hoped,
    but there's no link. The page turns to about:blank.

    Apparently it doesn't fetches the custom value. Could it be that it's outside the loop? The code is a tweak to a php document of a plugin (that resides in the wp_content/plugin folder.)

    I've read about the following that might help:
    <?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'Your-Custom-Field', true); wp_reset_query(); ?>

    except when I change this into

    global $wp_query;
    $postid = $wp_query->post->ID;
    $link = get_post_meta( $post->ID, "LINK", true );
    $onclick = "window.open( '$link' )";
    $title = '<a onClick="' . $onclick . '" href="#' . $id . '">' . $title . '</a>';
    wp_reset_query();

    It returns the same result (it opens a blank tab)

    Any idea?

  6. Big Bagel
    Member
    Posted 7 months ago #

    Yes, using $post->ID outside the loop is problematic. Your change didn't work because you're still using $post->ID in get_post_meta(). Try changing it to this:

    global $wp_query;
    $postid = $wp_query->post->ID;
    $link = get_post_meta( $postid, 'LINK', true );
    $onclick = "window.open( '$link' )";
    $title = '<a onClick="' . $onclick . '" href="#' . $id . '">' . $title . '</a>';

    You don't really need to call wp_reset_query() since you're not altering the main query/loop in any way. Also, $wp_query->post->ID returns the ID of the last post queried, so you should only use this when displaying a single post/page. If you ever want to use this in the loop, the previous code I suggested, with $post->ID, should work.

Reply

You must log in to post.

About this Topic