Home > plugin development > How to get Post ID with the Add Filter Function

How to get Post ID with the Add Filter Function

May 5Hits:1
Advertisement

I'm writing a plugin and am trying to figure out how to add data to the end of a post based on some Custom Meta Information.

So i've done this so far:

add_filter('the_content', 'AppendMeta'); 

And here is the "AppendMeta" function

AppendMeta($content) {     echo $content; // Echo out post content     $PersonName = get_post_meta($post->ID, 'PersonName', true);     echo 'Person: ' . $PersonName; } 

That code works if I replace $post->ID with the id of the post, but I need it to work based on the post the user is currently navigating. How would I pass the post id in as a parameter? $post->ID doesn't work in this scenario and I can't find out why.

Answers

Nevermind, found out I can use get_the_ID();.

This function will return the post id inside the the_content filter. The function simply declares the global $post object and returns its ID.

add_filter('the_content', 'wpse51205_content')
wpse51205_content($content) {
    echo $content; // Echo out post content
    $PersonName = get_post_meta(get_the_ID(), 'PersonName', true);
    echo 'Person: ' . $PersonName;
}

If you don't want to use get_the_ID(), you simply need to declare the $post object global before using it:

add_filter('the_content', 'wpse51205_content')
wpse51205_content($content) {
    global $post;
    echo $content; // Echo out post content
    $PersonName = get_post_meta($post->ID), 'PersonName', true);
    echo 'Person: ' . $PersonName;
}

the_ID() is only supposed to be used inside the loop, else you can't rely on it. Better use the $GLOBALS['post'] object inside your function call.

Related Articles

  • How to get Post ID with the Add Filter FunctionMay 5

    I'm writing a plugin and am trying to figure out how to add data to the end of a post based on some Custom Meta Information. So i've done this so far: add_filter('the_content', 'AppendMeta'); And here is the "AppendMeta" function AppendMeta($con

  • Add filter function in media modal box

    Add filter function in media modal boxSeptember 6

    I'm trying to extend the media modal, but I cant' find any documentation / tutorials about it. I'm not a master of backbone too ;-) I want to add a select box for each taxonomy that is attached to the attachment post type. At the moment only one sele

  • Http Hanlder must be reset with each deployment. How can I add this functionality to the web.configMay 12

    My application is a dotnet 4 hybrid - MVC in some areas, web forms in others. This application was recently upgraded to dotnet 4 and includes a lot of older code and some mismatched parts. Unfortunately it includes a telerik component that requires m

  • Can I add custom functions to gcalctool?

    Can I add custom functions to gcalctool?October 21

    I've noticed an f(x) button in calculator's (gcalctool) advanced mode. The button opens a drop-down list with some predefined functions. Is it possible to define more? --------------Solutions------------- If you press F1 and read the help page about

  • Add filter 'wpautop' to meta box textareaApril 20

    I'm using -this tutorial- to add simple meta boxes to a custom post type. But the textarea meta boxes are spitting out content all as one big paragraph, with no <p> formatting applied. What code would add the -wpautop filter- to the meta box textare

  • Anything I can add to functions.php to make text widgets accept PHP?May 30

    Is there a way I can make text widgets accept PHP? Perhaps something I can add in to functions.php? I'm redesigning my site, and I was wondering why I didn't use widgets at all in my current design, but then I realized it was because I use a lot of P

  • How to add newsletter functionality in drupal 7 (just stable modules if possible)June 21

    i've searched a little and i needed to add newsletter functionality to a drupal 7 installation. I've found simplenews, which is in beta for drupal 7 e nothing more. I don't need anything really complicated, just this functionalities: guest user can s

  • SharePoint 2010 - Add filter to a list with PowerShellJuly 27

    How it is possible to add a filter to a list with PowerShell? (I want to add a filter to multiple lists) Is there any tutorial, articel or example? --------------Solutions------------- To simply add filter to default view of existing list you can use

  • Add more functions in GeditJanuary 10

    I'm trying out Ubuntu for the first time so I'm a very new but I've done a bit of searching and I want to get something pretty similar to Notepad++ Gedit so far seems to be the pretty similar except I like a feature in N++ where I can add more functi

  • How to add filter with 2 args?January 19

    I want to modify $path in the following filter. It has 1 input and 2 args. function documents_template( $template = '' ) { $path = DOCUMENTS_INCLUDES_DIR . '/document/' . $template; return apply_filters( 'document_template', $path, $template ); } Thi

  • Can I use Node to add email functionality to an existing site?February 2

    I have a client with an existing straight up HTML/JavaScript site. No back end of any kind. He wants to add email functionality to it and I was thinking of using Node to do this for two reasons: It seems like an extremely lightweight way to do it He'

  • Add filter menu to admin list of posts (of custom type) to filter posts by custom field valuesMarch 13

    This answer is very close to what I am looking to do, but instead I would like to specify a specific custom field and display a select menu of its available values. Thanks! --------------Solutions------------- Simple to do, first create the dropdown

  • In add filter criteria "user" not coming from FILTER list in add views

    In add filter criteria "user" not coming from FILTER list in add viewsJune 25

    I am following the book "The Definitive Guide to Drupal 7" where I am trying to add a new view. But when I tried to add filter,I can't find the User in the FILER list . I am very beginner to Drupal,though I know PHP . --------------Solutions----

  • Firefox keyboard shortcuts to menu items / add-on functionsJuly 7

    At the moment I'm using context menus a lot to access commands in Firefox, but I would like to replace this repetitive clicking and searching with keyboard shortcuts for the common tasks that I perform. How to assign keys to add-on functionality? E.g

  • How to add filter in "Comments" at the admin panel?

    How to add filter in "Comments" at the admin panel?August 29

    I found so many information and most of the tutorials only talk about adding filter in post/custom post only. I want to do something similar to this tutorial in 'Comments' area in admin panel. Add filter menu to admin list of posts (of custom type) t

  • Allow users to add custom functions to wordpress theme

    Allow users to add custom functions to wordpress themeFebruary 5

    I'd like to allow users to add functions to the theme I'm developing, but don't want users to change functions.php directly, as this file may be updated with theme updates. In other words, I'd like this to work in a similar fashion to custom CSS file

  • Add filter to Admin list for all custom post types by their custom taxonomiesJuly 25

    I want to add filter on every Custom post type that have taxonomy associated with it. I have googled but found code for only for single post type or for single taxonomy. --------------Solutions------------- I found a code which work only with single

  • All custom fields are missing in views 'add field' and 'add filter' forms (Drupal 7)August 2

    I'm experiencing some very bizarre behaviour on a live website. I've just added a new date field to a bunch of content types. I now need to add this as a filter criterion to a view. The problem is that when I go to add the filter, it doesn't show any

  • How to add a functionality to views bulk operations?January 17

    I want to use views bulk operations to be able to change all my products' taxonomy terms at once. I have my product managing view with views bulk operations set up and working but there is no operation for editing taxonomy terms under "Modify entity

  • How to add a function to web form submit button?February 11

    On a Drupal site, when a form submission is done. I need to trigger a SMS. I am unable to edit the form submit code. Please let me know how to add a function to webform submit button. So that on SUBMIT button click the SMS function get executed as we

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.479 s.