Home Forums General Issues Advanced Custom Fields in a Custom Text Widget


Advanced Custom Fields in a Custom Text Widget

  • I’ve been trying to integrate Advanced Custom Fields into a custom text widget in my sidebar (using the PHP Text Widget to be able to use PHP + HTML there). I’m just using text variables. When the code is sitting in a template file, like home.php, it works perfectly. However, when used in a custom text widget, it’s not ‘registering’ the variables and inserting them dynamically. What am I missing?

    Thanks in advance for your time!

  • Hi @jboveri

    Can you please provide screenshots / code examples of what you are using in the widget?


  • We have custom fields set up this way:

    screenshot text fields

    A PHP programmer initially set up the sidebar HTML/PHP code in the template pages (home.php, page.php, etc). In there, the custom fields work, the hyperlinks are generated correctly. The custom text fills in to match the logged in user’s custom fields, creating custom links.

    I’m trying to move back to a more standard usage of the WordPress sidebar, where some widgets can be changed in the sidebar, as normal.

    For the widget where the custom text will populate, I set up a Custom Text Widget, with a plugin to allow it to accept PHP/HTML. Then, I pasted the code in. The variables aren’t processing when used in this widget. They are generated as $my_goals, rather than the value for $my_goals filling in, for example.

    This is the code in the widget:

    <ul class="menu" id="menu-my-stuff">
    <?php $my_goals =  get_field('_my_goals', 'user_'. $author_id ); $my_goals = $my_goals ? $my_goals : '#';?>
    <li class="menu-item ">
    	<?php echo "<a target=blank href=$my_goals>my Goals</a>" ?>
    <?php $my_analytics =  get_field('_my_analytics', 'user_'. $author_id ); $my_analytics = $my_analytics ? $my_analytics : '#';?>
    <li class="menu-item ">
    	<?php echo "<a target=blank href=$my_analytics>my Analytics</a>" ?>
    <li class="menu-item"><a href="" target="_blank">my Top Prospects</a></li>
    <?php $my_ad_scheduler =  get_field('_my_ad_scheduler', 'user_'. $author_id ); $my_ad_scheduler = $my_ad_scheduler ? $my_ad_scheduler : '#';?>
    <li class="menu-item ">
    	<?php echo "<a target=blank href=$my_ad_scheduler>my Ad Scheduler</a>" ?>
    <?php $my_commission_report =  get_field('_my_commission_report', 'user_'. $author_id ); $my_commission_report = $my_commission_report ? $my_commission_report : '#';?>
    <li class="menu-item ">
    	<a target=blank href="[acf field="{$my_commission_report}"]">my Commission Report </a>
    	<?php echo "<a target=blank href=$my_commission_report>my Commission Report</a>" ?>
    	<?php echo( '<a href=$my_commission_report>Click Me</a>' ); ?>
    <p><?php the_field('$my_commission_report'); ?></p>
  • Hi @jboveri

    I can see 2 issues with your code:

    1. You use a variable called $author_id multiple times, yet it is not defined anywhere.
    2. You use the_field without specifying a $post_id param. This is fine if you are within the loop, but I beleive your code will not have access to the global $post object, so it won’t work. You may need to add global $post above your code, or use a function to find the ID and then use it in your the_field calls.

    Does that help?

  • Elliot,

    I’m pretty new at PHP, but you were able to point me in the right direction. I did need to define the variables in the widget, and now it works.

    <?php global $author_id ?>
    <?php $author_id = get_current_user_id(); ?> 

    Really appreciate your help and your time with this!

  • Hi,

    I want to create some custom, theme based, widgets using ACF also. My first stumbling block is probably the simplest! I wondered if you could point me in the right direction since you have previously done so.

    So far, I’ve setup a Custom Field, but where it says Location, rules: Show this field group if… what do I choose?!

    It’s probably something so simple!

    Thanks in advance

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

The topic ‘Advanced Custom Fields in a Custom Text Widget’ is closed to new replies.