Support

Account

Home Forums Backend Issues (wp-admin) Widget Instance Title as ACF Field?

Solving

Widget Instance Title as ACF Field?

  • I’ve managed to get widgets working with ACF. But what I’m wanting to do is that if a specific custom field within a widget is NOT empty, then I want the title of the instance’s meta box to show that title instead of the default widget title. (The reason for doing this is clear–if you have a bunch of instances of one widget type, it’s nice to be able to see which widget is which without having to open them all, right?) As it stands currently, every widget just says “Photo BG Widget”, and I’d like them to say “Photo BG Widget: $title” essentially.

    In my functions.php I have the following. I know I need to change the the update and form methods, but I just can’t figure out the syntax to pull the value of that ACF field (cf-widget-heading-01) and to essentially tell the widget that if it’s blank, use the default title, but if there’s a value, append that to the widget title.

    Help?

    
    class PhotoBG_Widget extends WP_Widget
    {
      function PhotoBG_Widget()
      {
        parent::WP_Widget(
          false, // Base ID
          __( 'Photo BG Widget', 'text_domain' ),
          array( 'description' => __( 'If your widget needs a bg image, this is the widget for you.', 'text_domain' ))
        );
      }
    
      function update($new_instance, $old_instance)
      {
    //syntax not working
    //$title = get_field('cf-widget-heading-01', $instance['ID']);
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance[$title]);
        return $instance;
      }
    
      function form($instance)
      {
    //syntax not working
    //$title = get_field('cf-widget-heading-01', $instance['ID']);
        $title = esc_attr($title);
        echo "<br />";
      }
    
      function widget($args, $instance)
      {
        $widget_id = "widget_" . $args["widget_id"];
        include(realpath(dirname(__FILE__)) . "/library/widgets/photobg-widget.php");
      }
    }
    register_widget("PhotoBG_Widget");
  • Did you try:

    
    $title = get_field('cf-widget-heading-01', 'widget_' . $widget_id);
    

    That worked for me. Hope that helps!

  • I dug into the code and found that WP is looking for a field which matches this jQuery selector input[id*="-title"] when appending the widget title, so you have two choices:

    1) Change the key of the custom field so that it ends in the string “-title”. This only seems possible if you’re registering your custom fields with code, not through the admin interface.

    2) Add the title field manually in your widget’s form() method (and widget() method if you’re displaying it on the frontend). I reused some code I found in default-widgets with good results.

    Good luck,
    Marc

  • Hi @watermelonkid

    I’m trying to do the same thing. Have you ever got it working?

  • Hi @watermelonkid

    Just like @jonahcoyote suggested you need to change your syntax for the ID to widget_<id>. Do that and it should work!

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

The topic ‘Widget Instance Title as ACF Field?’ is closed to new replies.