0

I am using `ACF` plugin to add a custom field to my widgets. After creating the field with the name `mobile_enable`, tried to get the value with the method explained in the ACF docs which is the following:
the_field('field_name', 'widget_' . $widget_id)

Inside the widget method we can get the widget_id like this:

public function widget( $args, $instance ) {
   $widget_id = $args['widget_id']
}

The problem I am having is that the following code returns null even after enabling the option with the checkbox:

$is_mobile_enabled = get_field('mobile_enable', 'widget_' . $args['widget_id']);
var_dump($is_mobile_enabled);
2
  • In your last snippet: 'mobile_enable' are you missing a "d"? Commented Dec 28, 2023 at 10:03
  • No that is how I defined it 😅
    – MeMo
    Commented Dec 28, 2023 at 15:51

1 Answer 1

-1

We can't get the whole picture unless we see exactly where your code has been added and called, but I suggest to check every variables value of this line of code $is_mobile_enabled = get_field('mobile_enable', 'widget_' . $args['widget_id']);

  • Check the value of $args['widget_id'], as if it may be empty.
  • Also, it's better to use error_logs instead of var_dump, you can do it something like this error_log(print_r($is_mobile_enabled,true));
1
  • Thanks or the reply but I have checked every variable and they are all correct. I will use your method to see if anything new happens
    – MeMo
    Commented Dec 28, 2023 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.