Title: Accessing arguments in Custom Variable callback
Last modified: November 4, 2023

---

# Accessing arguments in Custom Variable callback

 *  Resolved [plusless](https://wordpress.org/support/users/plusless/)
 * (@plusless)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/accessing-arguments-in-custom-variable-callback/)
 * Hello,
 * I’m programatically [creating custom variables](https://rankmath.com/kb/variables-in-seo-title-description/#add-code-snippet)
   using an array and a foreach loop. The array is a list of platforms which corresponds
   with an meta key in the database which stores the url of the said platform for
   the current post.
 *     ```wp-block-code
       $platform_list = array(
          array('name' => 'Apple Music', 'slug' => 'apple_music')
          array('name' => 'Soundcloud', 'slug' => 'soundcloud'),
          array('name' => 'Spotify', 'slug' => 'spotify')
       );
   
       foreach($platform_list as $platform) {
          $slug = $platform['slug'];
          $name = $platform['name'];
   
          rank_math_register_var_replacement($slug, array(
             'name' => esc_html__($name, 'rank-math'),
             'description' => esc_html__('url for '.$name, 'rank-math'),
             'variable' => $slug,
             'example' => 'test'
          ), 'my_callback_function'); 
       }
   
       function my_callback_function($var_args = array(), $post = array()) {
             var_dump($var_args);
             return '#url';
       }
       ```
   
 * Using a foreach loop to create custom variables works. However I like to access
   the arguments I pass into _rank\_math\_register\_var\_replacement_ inside my 
   callback function, because I need the value of _$slug_ in order to get the corresponding
   metadata.
 * The problem is that $var_args is always empty in the callback function.
 * **I have tried the following:**
    - Setting the callback as a function instead of a string and passing the $slug
      variable. This does not work because Rank Math uses _call\_user\_func_ to 
      run the callback function (includes/replace-variables/class-variable.php:188).
    - Using _$this_ (like Rank Math its build-in variables) is not possible because
      we are outside of the object scope.
 * **Relevant functions within Rank Math:**
    - rank_math_register_var_replacement, template-tags.php:83
    - register_replacement, class-manager.php:82
    - run_callback, class-variable.php:185
 * **The question:**
 * How can I access the arguments of _rank\_math\_register\_var\_replacement_ inside
   the callback function or pass an argument into the callback function?
 * Thanks!

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

 *  Plugin Support [Rank Math Support](https://wordpress.org/support/users/rankmathsupport/)
 * (@rankmathsupport)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/accessing-arguments-in-custom-variable-callback/#post-17183183)
 * Hello [@plusless](https://wordpress.org/support/users/plusless/),
 * Thank you for contacting support.
 * The arguments that you are passing on to the `rank_math_register_var_replacement()`
   function is only available to it because you are inside the foreach loop you 
   have created to wrap the function and since the callback function is outside 
   that loop the variables are not available to it as they are not global-scoped
   variables.
 * It’s possible to pass arguments to the callback function and in our own source
   code we do that, especially for advanced variables such as the custom fields 
   one where it accepts one argument (custom field name) as you can see here: https://
   github.com/rankmath/seo-by-rank-math/blob/master/includes/replace-variables/class-
   advanced-variables.php#L243
 * In your case, you can do the same and pass any argument you want, you only need
   to make sure that the variable you are passing to the callback is available in
   the same scope.
 * Don’t hesitate to get in touch if you have any other questions.
 *  Thread Starter [plusless](https://wordpress.org/support/users/plusless/)
 * (@plusless)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/accessing-arguments-in-custom-variable-callback/#post-17185020)
 * Thanks!
 * I got it working with a class, for future reference here is how I did it:
 *     ```wp-block-code
       $platform_list = array(
          array('name' => 'Apple Music', 'slug' => 'apple_music')
          array('name' => 'Soundcloud', 'slug' => 'soundcloud'),
          array('name' => 'Spotify', 'slug' => 'spotify')
       );
   
       foreach($platform_list as $platform) {
          $variable = new platform_variable($platform);
       }
   
       class platform_variable {
          protected $slug, $name;
   
          function __construct($platform) {
             $this->slug = $platform['slug'];
             $this->name = $platform['name'];
   
             rank_math_register_var_replacement($this->slug, array(
                   'name' => esc_html__($this->name, 'rank_math'),
                   'description' => esc_html__('url for '.$this->name, 'rank_math'),
                   'variable' => $this->slug,
                   'example' => $this->my_callback_function()
                ),
                [ $this, 'my_callback_function' ]
             );
          }
   
          function my_callback_function() {
             $post_id = get_the_ID();
             $metadata = get_post_meta($post_id, $this->slug, true);
   
             if (!empty($metadata)) {
                return esc_attr($metadata);
             } else {
                return null;
             }
          }
       }
       ```
   
 *  Plugin Support [Rank Math Support](https://wordpress.org/support/users/rankmathsupport/)
 * (@rankmathsupport)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/accessing-arguments-in-custom-variable-callback/#post-17191404)
 * Hello [@plusless](https://wordpress.org/support/users/plusless/),
 * Glad that helped.
 * If it isn’t too much to ask for, would you mind leaving us a review here?
   [https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post](https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post)
 * It only takes a couple of minutes but helps us tremendously.
 * It would mean so much to us and would go a long way.
 * Thank you.

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

The topic ‘Accessing arguments in Custom Variable callback’ is closed to new replies.

 * ![](https://ps.w.org/seo-by-rank-math/assets/icon.svg?rev=3438330)
 * [Rank Math SEO – AI SEO Tools to Dominate SEO Rankings](https://wordpress.org/plugins/seo-by-rank-math/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/seo-by-rank-math/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/seo-by-rank-math/)
 * [Active Topics](https://wordpress.org/support/plugin/seo-by-rank-math/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/seo-by-rank-math/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/seo-by-rank-math/reviews/)

## Tags

 * [arguments](https://wordpress.org/support/topic-tag/arguments/)
 * [callback](https://wordpress.org/support/topic-tag/callback/)
 * [foreach](https://wordpress.org/support/topic-tag/foreach/)
 * [Replacement](https://wordpress.org/support/topic-tag/replacement/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * 3 replies
 * 2 participants
 * Last reply from: [Rank Math Support](https://wordpress.org/support/users/rankmathsupport/)
 * Last activity: [2 years, 7 months ago](https://wordpress.org/support/topic/accessing-arguments-in-custom-variable-callback/#post-17191404)
 * Status: resolved