WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Adding Message Depending on Custom Field (9 posts)

  1. Tom Morton
    Member
    Posted 4 years ago #

    So my homepage is a page within wordpress. It has a custom field that is defined by custom meta boxes. Essentially, at all times the custom field of the homepage will be either LIVE or STANDARD.

    I want to write a function in my functions.php file that will allow me to show an "error" or "message" box (Ala All In One SEO, or when you save a post) that will notify anyone logged in that the custom meta is X.

    So if the specific page (id=476) has a custom field "broadcast" that has the variable "live", it shows an error or message box across the admin.

    ::EDIT:: So I forgot to mention that I'm trying to get this message to appear in the WP-admin not the normal site ::/EDIT::

  2. Ryan Paul
    Member
    Posted 4 years ago #

    I'm curious as to what your desired result is?

  3. Tom Morton
    Member
    Posted 4 years ago #

    Thanks for the reply. Here's whats going on. I'm live streaming a show, but we don't live stream all the time. When we are live streaming, I have a custom template that loads. I enable this using a custom field set on my "homepage" page.

    I want to check IF the custom field ='s "live", then execute the admin_notice that I have written. If it does not = live, then do nothing.

    This gives a message to anyone logged into the admin panel that live streaming is enabled.

  4. Tom Morton
    Member
    Posted 4 years ago #

    Just to give a little more detail, I've got this going in my functions.php so far. Everything works except for checking what the custom field is. Right now, it doesn't matter if the custom field = live or standard, it shows the error alert regardless.

    global $wp_query;
    $postid = '476';
    $customField = get_post_meta($postid, 'broadcast', true);
    
    if(isset($customField['live'])){
    
    function live_message(){
    echo '<div class="error fade" style="background-color:#FFEBE8;">
        <p style="font-size:13px;"><strong>You are Broadcasting Live!</strong></p>
        <p><a href="' . admin_url( '/post.php?post=476&action=edit' ) . '">Click here</a> to edit the homepage and turn live streaming mode off. If you want to continue to live stream, ignore this message!</p>
    
        </div>';
    
    }    add_action( 'admin_notices', 'live_message');
    
    } else {}
  5. MichaelH
    Member
    Posted 4 years ago #

    Didn't test this but:

    function my_admin_notice() {
    $pageid = 476 ;
    $broadcast = get_post_meta($pageid, 'broadcast', true);
    if ($broadcast == 'live') {
        echo "
        <div id='my_admin_notice' class='updated fade'><p><strong>".__('This is my message.')."</strong> ".sprintf(__('Hello there.'))."</p></div>
           ";
      }
    }
    add_action('admin_notices', 'my_admin_notice');
    return;
  6. Tom Morton
    Member
    Posted 4 years ago #

    @MichaelH I honestly don't know how you got so good at WordPress, but it seems like you always come through. I really appreciate it man, thanks so much!

    Its guys like you who motivate me to get in the forms and help out, even if I'm not exactly at "guru" status just yet.

    Thanks again!

  7. Ryan Paul
    Member
    Posted 4 years ago #

    Before I post a solution - do you want a message to show that says 'You're off air' or something like that? Or nothing to show at all when the box is empty?

  8. Ryan Paul
    Member
    Posted 4 years ago #

    Yeah that's pretty much what I would've done (almost)

  9. Tom Morton
    Member
    Posted 4 years ago #

    Two questions for you guys. One, the 'return;' line breaks my sidebar, basically gives me an error saying "your theme does not support sidebar widgets". I deleted it, and everything still works. Is that cool or dangerous?

    Two, I would assume the same technique could be used for the front end as well. Example, check if the custom field ='s live, and if so display the live embed stream?

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic