• I’m adding a filter in functions.php and I’m a bit stumped why I can’t get things to work. Have been pulling my hair out for a few days now 🙂 Not sure if it’s a WordPress or PHP issue, but I can’t figure it out.

    In the original function, one can get the user ID with:

    {marker_id}

    I can output this in my filter and get a user ID 111. I can also add it to a variable and then output that variable:

    $user_id = ‘{marker_id}’;

    So far, so good. The problems start when I try to use this user ID inside my filter:

    function wpgmp_location_criteria($infowindow_setting, $map) {
    
      $user_id = '{marker_id}';
      $user_url_A = get_author_posts_url('{marker_id}');
      $user_url_B = get_author_posts_url($user_id);
      $user_url_C = get_author_posts_url('111');
    
       $infowindow_setting .= '#1: ' . $user_id . '<br />';
       $infowindow_setting .= '#2: ' . $user_url_A . '<br />';
       $infowindow_setting .= '#3: ' . $user_url_B . '<br />';
       $infowindow_setting .= '#4: ' . $user_url_C . '<br />';
    
      return $infowindow_setting;
    }
    add_filter('wpgmp_infowindow_message', 'wpgmp_location_criteria', 1, 2 );

    As can be seen in this example, I’m trying to get the user posts URL. On #1 I get the user ID just fine. On #4 I hardcode the user ID and that gets the correct URL:

    https://mydomain.com/authors/author-name/

    But for #2 and #3 I only get:

    https://mydomain.com/authors/

    And this is what I can’t figure out. The {marker_id} and $user_id that I use in #2 and #3 output the correct user ID if I output them directly, but not when using them in the get_author_posts_url() function.

    Is this due to how add_filter() works, how PHP deals with numbers, or.. I can’t figure it out!

    • This topic was modified 3 years, 8 months ago by KS.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Howdy,

    From the looks of the hook name it appears to be a plugin hook, correct? Have you tried reaching out to the plugin developer to see if they might be able to help as well?

    First thing that came to mind was checking to see what that core function accepts ( https://developer.ww.wp.xz.cn/reference/functions/get_author_posts_url/ ) and it does look like it casts things to an integer so it might be that?

    Thread Starter KS

    (@karl19)

    Hi @jcastaneda, many thanks for the reply! I’ve talked to the developers, but for now I haven’t gotten any further with this issue.

    I think you might be on to something with integers. I’m not very good with PHP, but if I run gettype, it says {marker_id} is a string. I can’t figure out how to convert it though (if that’s what I need to do). Trying:

    intval($str);
    (int) $str;
    (integer) $str;

    all just return 0. From what I can read online, I can’t quite figure out why that’s the case.

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    So I guess the question here is how are you getting that {marker_id}? I can only really guess it’s from the plugin being used and going to take a shot in the dark and say this plugin: https://ww.wp.xz.cn/plugins/wp-google-maps/ ?

    Moderator bcworkz

    (@bcworkz)

    {marker_id} may carry a value in JavaScript or where it came from, but in PHP it’s just a string, there’s no underlying value you can extract. The ID you want can only come for either $infowindow_setting, $map, or some global or static variable.

    Thread Starter KS

    (@karl19)

    Many thanks for the input, both of you! I think you’re right, {marker_id} seems to be a JavaScript value.

    I dug deeper and found there is also a $user_id variable that contains the ID. But this also seems to be a string, and I can’t convert it into an integer. All very confusing to me, I’m hoping to get some more feedback from developers as well. But thanks for bringing attention to the value perhaps just coming through JavaScript.

    Moderator bcworkz

    (@bcworkz)

    If the ID string value is numeric, it likely can be used in PHP as though it’s an integer. PHP is loosely typed, type often doesn’t matter. When it does matter, it can be type cast like so: $user_int = (int) $user_id;

    If the ID is alphanumeric, type casting would be inaccurate. It also wouldn’t be a WP user ID since they are numeric only.

    Thread Starter KS

    (@karl19)

    @bcworkz Yes that’s what I thought too, that it would be fine whatever the type of value. But I think you were right about it just being JavaScript.

    I managed to get some input from the developers and they showed me a different wy of getting the user ID, looks like this:

    foreach($map_data['places'] as $key => $place){
      if(isset($place['id'])){

    Thanks again for having a look at this and making me realise it’s not necessarily a variable just because something gets output that looks like it could be used as a variable!

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘add_filter and calling WP functions’ is closed to new replies.