• Resolved Schmee

    (@schmee)


    Hello,

    I am trying to employ an add_action (via my child’s functions.php) in order to add a meta property to my pages.

    Being a very novice PHP user, am running into the simple issue of not knowing how to include the required single quotes around “featured_image” and “url” below.

    (for those curious, this simply sets the OG:IMAGE property to a page’s featured image.

    Any solutions would be appreciated – thank you!

    ===================

    add_action('wp_head', 'my_ogimage');
    function my_ogimage() {
    echo '<meta property="og:image" content="<?php $featuredImage = get_field('featured_image'); echo esc_url( $featuredImage['url'] ); ?>" />';
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • There are more issues than that with this code.

    Here is a rewrite of your single line of code, into several.

    Your line:

    echo '<meta property="og:image" content="<?php $featuredImage = get_field('featured_image'); echo esc_url( $featuredImage['url'] ); ?>" />';

    How you should be writing it:

    echo '<meta property="og:image" content="';
    $featuredImage = get_field('featured_image');
    echo esc_url( $featuredImage['url'] );
    echo '" />';

    Thread Starter Schmee

    (@schmee)

    Jonrdaio – thank you.

    However when I save this to my php file it white-screens my site with no reported error.

    add_action('wp_head', 'my_ogimage');
    function my_ogimage() {
    echo '<meta property="og:image" content="';
    $featuredImage = get_field('featured_image');
    echo esc_url($featuredImage['url'] );
    echo '" />';
    }

    Note that I am trying to echo the answer from this page.

    They are referring to an entry of a meta-property (directly placed in the head of a wordpress page) however I would like to insert it via an ‘add_action’ from my child-theme’s function.php file.

    The first thing to try for the WSOD (White Screen of Death) is setting WP_DEBUG to TRUE in wp-config.php. That will usually provide you with an error message.

    As for why it would not work, I don’t have any experience in that area.

    Moderator bcworkz

    (@bcworkz)

    I suspect a few things. If you were to hardcode the opengraph metatag in your header.php file (temporarily, remove it later) without any PHP at all, does your featured image display? The OP in the other post has a specific plugin to handle this, it is not part of the default WP installation. If the image displays, continue, otherwise this solution is not for you, but you might try searching for “featured image” in the Codex.

    Once you’ve confirmed the image displays with the hardcoded opengraph meta tag, you may remove it. Do you have Advanced Custom Fields plugin installed? If not, get_field() is not a valid function. Instead of using the plugin, you could store the URL as a normal WP custom field and retrieve it with get_post_meta(). In either case, ‘featured_image’ is an arbitrary label. Have you stored your image URL under this label? Your label and that in the code must match.

    In this particular code, the stored value is an associative array and the actual URL is under the key ‘url’. You must use ACF and store such an array for this to work. The default WP custom fields do not accept arrays, the only value you can store would be the URL itself, so you would drop the ['url'] portion of the echo line.

    IMO, the default WP featured image facility works reasonably well. Many people have good reasons to implement something else such a opengraph. If you also have such a reason, fine. Otherwise consider using the WP featured image facility.

    Thread Starter Schmee

    (@schmee)

    Thank you all for your input – I will follow the above pointers.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Embedded single-quote question’ is closed to new replies.