Home Forums Add-ons Options Page Combine Two PHP Scripts


Combine Two PHP Scripts

  • I have a option field set and named ‘adwords_cost_for_michigan’

    I enter in a total amount in the admin and wish to call it from my template php code.

    I know that this would work.

    <?php echo the_field(‘adwords_cost_for_michigan’, ‘option’ ); ?>

    however I would like this to be dynamic and call in the username of the person who is logged in.

    So my code on the template would be something like this.

    <?php echo the_field(‘adwords_cost_for_*php call for username*’, ‘option’ ); ?>

    This way since I have a few hundred users on my site I can just use one piece of code instead of hardcoding each call for each location.

    Is it possible to call in the username of the user inside of your (‘field-name’)?

  • Hi,

    it’s simple, just create the variable first (to keep it clean) and then use it instead of the field name, like this:

    $username = 'John'; // get this yourself
    $dynamic_field_name = 'adwords_cost_for_' . $username;
    echo the_field($dynamic_field_name, 'option');
  • Does each user have their own options page? Or that is what you’re looking for, so that each person can create their own values for all the options?

    In order to have all those custom field names you’d have to create them dynamically in the first place. First you need to export the field group from ACF and then make modification to it.

    So, first you’d need to generate all the field groups dynamically based on the logged in user.

     $username = 'user_1'; // whatever code you use to get the user name
     if(function_exists('register_field_group')) {
      $field_group = array(
       'id' => 'acf_adwords',
       'title' => 'Adwords',
       'fields' => array(
         'key' => '_key_adwords_cost_for_'.$username,
         'label' => 'Adwords Cost For '.$username,
         'name' => 'adwords_cost_for_'.$username,
         'type' => 'text',
         'default_value' => '',
         'placeholder' => '',
         'prepend' => '',
         'append' => '',
         'formatting' => 'none',
         'maxlength' => '',
       'location' => array(
          'param' => 'options_page',
          'operator' => '==',
          'value' => 'acf-options-my-options-page',
          'order_no' => 0,
          'group_no' => 0,
       'options' => array(
        'position' => 'normal',
        'layout' => 'no_box',
        'hide_on_screen' => array(),
       'menu_order' => 0,

    Now that you’ve generated a dynamic option name based on the logged in user you can retrieve the value based on the logged in user.

    That’s going to make for a lot of options in your option table.

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

The topic ‘Combine Two PHP Scripts’ is closed to new replies.