Support

Account

Home Forums Search Search Results for 'q'

Search Results for 'q'

reply

  • I don’t know if this will help you or make your dev process any easier. My goal when I build a site has to do with making things easy for the client and not necessarily making it easier for myself, so when I’m building the admin for the client I don’t even give much thought to how difficult it will be for me to code, I just make sure that it’s something that can be coded. However, I do my best to never code something more than once.

    I build in “modules” I have a field group that has all the settings I need for links, not only the ones you mention but others. I then clone this field group as the only sub field of a “Group Field”, this insures that all fields of my group are always “sub fields” With this in place I have a single template part file that is called to output links.

    
    // something like this goes into my template
    if (have_rows('group_field_name')) {
      // always true for a group field that exists
      while (have_rows('group_field_name')) {
        // always executes once for group fields that exist
        get_template_part('template-parts/components/link);
      }
    }
    

    So I have only coded this once. My group has become quite complicated, but if changes are made I only need to make them in a single file.

    The field group for links can also be cloned as the only sub field in a repeater and the template part file will still work but allow the client to add multiple CTA buttons.

    I actually use a true/false field the new window setting. The label on the true false field is
    – Same Window = false
    – New Window = true
    and the code in my template part for building this part of the link button is

     
    $target = '';
    if (get_sub_field('target')) {
      $target = ' target="_blank"';
    }
    

    and example of my link output

    
    <a href="<?php echo $url; ?>" class="<?php echo implode(' ', $cta_classes) ?>"<?php echo $title.$rel.$target.$onclick; ?>><?php echo $text; ?></a>
    

    Also, when it comes to a URL and the client entering a URL, I do not use a URL field because it is too limiting. For example lets say that you want to allow the client to enter anything other than a URL. My clients and the people I work with want to be able to enter anchor links as well as tel:, mailto:, sms: and ftp: links. So instead of using a URL field I use a text field with custom validation. Building separate fields would increase the code and complicate things for the client. Instead I have a text field that will accept anything that appears to be a valid href value and as part of the client’s instruction when handing over the site they are shown how to use this field and the field has very good instructions to help remind them.

    
    add_filter('acf/validate_value/key=field_XXXXXXXX', 'validate_text_as_href', 20, 4);
    function validate_text_as_href($valid, $value, $field, $input) {
      // this allows links starting with
      // / (site root relative), http://, https://, ftp://, # (anchor), mailto:, tel:, sms:
      if (!$valid) {
        return $valid;
      }
      if (!empty($value)) {
        if (!preg_match('%^(https?\://|ftp\://|/|#|mailto\:|sms\:|tel\:)%', $value)) {
          $valid = 'Enter a Valid HREF Value';
        }
      }
      return $valid;
    }
    

    As to automatically updating or redirection, I have not built anything that will automatically redirect if the slug of a page is changed. I will need to look into that, what I do know is that what is built into WP rarely works as expected.

    As far as automatically updating links when a slug or url changes, you would need to keep some type of history on the slug/url of the page when it is updated. You would need to add a pre_post_update action that somehow checks to see if the url of the page is being changed. If it is then you’d have to figure out how to lookup and changed all of the other posts and pages where there is a link to the page being updated and change them without this process timing out the admin page refresh. This would basically mean that you’d have to record somewhere/somehow all of the places where a link to the page is saved to allow a quicker lookup so that they can be found quickly and updated.

    This brings me back to the automatic redirection because the testing to see if the URL has changed for a page sparked an idea. You could as in the above add a pre_post_update action and if possible record a history of all of the URLs that were ever assigned to this post and then on a 404 you could do a search for posts that have had the missing URL. This isn’t actually a bad idea that I may look into.

  • You did not really answer my question about more detail.

    What I am assuming is that

    1. You load data into a field that can be selected
    2. Based on this selection you add choices to a second field or multiple other fields for selection
    3. Repeat if additional fields are dependent on the selection of the first field

    I have some examples of doing this here https://github.com/Hube2/acf-dynamic-ajax-select-example

    Basically what you are looking at is building a JavaScript/AJAX application that does the requests and population of choices in addition to adding PHP that loads the initial choices of the first field.

    What I would do is that I would create a cusome post type to hold posts returned by the API and then I’d probably use a post object field to allow the user to select on of those posts rather than try to store all of this information in fields.

  • I don’t know if I said this before, but a page link field located in one place is not aware of changes to the slug for a page. The link field stores the current url when it is updated.

    This same thing will happen when you add a link in WP to the content area. If you create a link in the WYSIWYG using the WP add link filter you will experience the same issue of you then change the slug of the page you are linking to.

    ACF uses the built in WP link field and the ACF field is just a wrapper for WP functionality that already exists. In order to have the field and links updated when a slug is changed would require searching the content and all custom fields for every post to find the old link and replace it with a new link.

    The solution I employ is to install a redirect plugin, there are several available. I work at an SEO company and everyone here knows that when a URL on the site is altered that 301 redirect must be added. This is because in order to get search engines to index the new page instead of the old page that these redirects need to be put in place. A side effect of using a redirect tool is that these links that get broken by changing a slug do not result in a 404 page. My company also employs tool for our clients that scan internal links to find 404s and redirected pages so that these can be corrected later.

    Automating the updating of all links pointing to a page when a page slug is changed would be extremely difficult, if not impossible to achieve.

  • Dear All,

    If I understand this thread correctly, the implication is that the ACF isn’t allowing the blocks js to load? Is that correct?

    I’m running WordPress 5.8 and Enfold 4.8.5. I last used the Block Editor on July 11th… but now it no longer appears when I try and create a new post.

    Here’s a screenshot of what I see when I try to edit a post with my ACF’s:
    https://www.dropbox.com/s/dlqc3wcyrndg3jx/Screen%20Shot%202021-08-01%20at%2013.27.18.png?dl=0

    So if I’ve understood this thread correctly, is there yet a fix for this?

    Thank you.

  • I have the same code and it works for me. The js file appears in the footer (back-end/gutenberg and front-end). Maybe try this:

    
    function my_init() {
    
            wp_register_style( 'team', get_template_directory_uri() . '/dist/blocks/css/team.css', array(), filemtime( get_template_directory() . '/dist/blocks/css/team.css' ), 'all' );
            wp_register_script( 'team', get_template_directory_uri() . '/dist/blocks/js/team.js', array( 'jquery', 'application' ), filemtime( get_template_directory() . '/dist/blocks/js/team.js' ), true );
    }
    add_action('init', 'my_init');
    
    // acf_register_block_type:
    'enqueue_assets'    => function () {
            wp_enqueue_style( 'team' );
            wp_enqueue_script( 'team' );
          },
    
  • The issue is no JS files are being loaded from the blocks. The CSS is being loaded for example when viewing page source I see:
    <link rel='stylesheet' id='testimonials-css' href='http://enosix.localhost/wp-content/themes/enosix/dist/blocks/css/testimonials.css?ver=1627702841' media='all' />

    But there is no JS file. So that’s the issue. Does anyone have an example of working JS in the preview? It would seem that

    'enqueue_assets'    => function () {
            wp_enqueue_style( 'team', get_template_directory_uri() . '/dist/blocks/css/team.css', array(), filemtime( get_template_directory() . '/dist/blocks/css/team.css' ), 'all' );
            wp_enqueue_script( 'team', get_template_directory_uri() . '/dist/blocks/js/team.js', array( 'jquery', 'application' ), filemtime( get_template_directory() . '/dist/blocks/js/team.js' ), true );
          },

    Is not working for JS.

  • I just found a way to do it

     <?php $term = get_field('coffee_type_image', 'coffee_type_' . $wcatTerm->term_id);?>
     <a href="<?php echo get_term_link($wcatTerm->slug, $wcatTerm->taxonomy); ?>"><img src="<?php echo $term; ?>" alt="" class="img-responsive"></a>

    This works

  • Yes I’ve followed this to my knowledge. Here are a few examples of my js.

    features.js

    (function($) {
      var initializeBlock = function( $block ) {
        function matchHeightFire() {
          $('.featureBox-icon').matchHeight({property: 'min-height'})
          $('.featureBox-content').matchHeight({property: 'min-height'})
          $('.featureBox-title').matchHeight({property: 'min-height'})
          $('.featureBox').matchHeight({property: 'min-height'})
        }
        matchHeightFire()
      }
    
      $( document ).ready(function() {
        $('.block--features').each(function() {
          initializeBlock( $(this) );
        })
      })
    
      if( window.acf ) {
        window.acf.addAction( 'render_block_preview/type=features', initializeBlock );
      }
    })( jQuery );

    testimonials.js

    (function($) {
      var initializeBlock = function( $block ) {
        console.log('testimonials loaded')
        setTimeout(function () {
          $( '.testimonials' ).slick(
            {
              dots: false,
              infinite: true,
              arrows: false,
              autoplay: true,
              autoplaySpeed: 8000,
              slidesToShow: 1,
              adaptiveHeight: true,
              //centerMode: false,
              variableWidth: false,
              fade: true,
              cssEase: 'linear'
            }
          )
        }, 0);
    
        $('.testimonials').on('beforeChange', function(event, slick, currentSlide, nextSlide) {
          $('.testimonialsNav-show').removeClass('active')
          $(this).closest('.testimonialsBox').siblings('.testimonialsNav').find('.testimonialsNav-show[data-slide="' + nextSlide + '"]').addClass('active')
        });
    
        $('.testimonialsNav-show[data-slide]').click(function(e) {
          e.preventDefault();
    
          $('.testimonialsNav-show').removeClass('active')
          $(this).addClass('active')
    
          var slideno = $(this).data('slide');
          $(this).parent().siblings('.testimonialsBox').find('.testimonials').slick('slickGoTo', slideno);
        });
      }
    
      $( document ).ready(function() {
        $('.block--testimonials').each(function(){
          initializeBlock( $(this) );
        });
      })
    
      if( window.acf ) {
        window.acf.addAction( 'render_block_preview/type=testimonials', initializeBlock );
      }
    })( jQuery );
    
  • There was an error in the code above. Below is the correct one:

    acf_register_block( array(
    	'name'			=> 'my-custom',
    	//...
    	'enqueue_assets' => function () {
    		wp_enqueue_script('my-script');
    	}
    ));
    
    function my_function() {
    	wp_register_script('my-script', get_template_directory_uri() . '/js/my-script.js', array(), '1.0.0', true);
    }
    // The script is placed only on the frontend - OK
    add_action('wp_enqueue_scripts', 'my_function');
    // The script is placed only on the frontend - Why?
    add_action('enqueue_block_assets', 'my_function');
    // It works! The script is placed on the Gutenberg and on the frontend
    add_action('init', 'my_function');
    
  • I believe I’m right in saying that you can only include the <InnerBlocks /> tag once per block, so it wouldn’t really working in the repeater situation.

    However, I believe you can still get the desired result with a slightly different approach. You’ll just need to use two custom blocks instead.

    The first block, “tab group”, will look something like this:

    <div class="group"> <InnerBlocks /> </div>

    The second block, “tab item”, will look something like this:

    <div class="item"> <InnerBlock /> </div>

    The key is when you set the “tab group” block up that you define 'allowed_blocks' to only allow “tab item” (acf/tab-item). Also, you can set the “tab item” up with 'parent' to only allow it as an option when inside a “tab group”. Now, when you’re in the editor, you can firstly create a group block and then add as many unique item blocks to the group. Basically the same as a repeater!

  • As this is article comes up a lot in search results, I wanted to add my experiences.

    I prefer coding to plugins whenever it makes sense. Without a plugin, I was not able to search both the post title and the ACF fields for a custom post type. Making changes to functions.php and/or creating a custom WP_Query, I just couldn’t get there.

    Using ACF: Better Search, I found it includes ACF fields on the search result page (search.php) but doesn’t work for custom post types (archive-my_post_type.php) or custom WP_Query.

    I installed Relevanssi and it offers a simple way to include post types and custom fields. I ran the index and it works perfectly for me.

  • I am not familiar with the settings for this plugin. I would think that an import plugin could do updates as well, but again, I do not know how the plugin works. The best place to get an answer to your questions would be to talk to the developer of the other plugin.

  • i have the same problem, in my testing site (both pages are in the same server but differents instance) and the unique difference is in my testing site use the UI for build the form, then i export and add to the page where i want and the other important data is in the options page, i check the database in options table and yes, the fields are created and the value data is empty

  • That was my thought too but I can’t see any way for the logic to interact with the choices in the same field. As the categories are entered in the ACF admin interface it would ideally be something that is entered in the same field group as the choices, but there’s no obvious facility for adding additional fields to the Select field group.

    I suspect the only way to accomplish this the way I envisage it would be to customise ACF on a coding level, to add in the ability to add additional fields and have them linked to each choice in the select choices field, so they would be there along with the default fields such as Default Value, Instructions, Field Label and Field Name etc. Probably the most efficient method would be to have a second : on each line in the Choices box, so the first item is the value, then the label, then the line of text (which could possibly just be a pointer to some other field that the user creates).

    There’s possibly another way to get the same result, adding in those other text fields manually and then dragging out the field contents using some code in the WordPress functions or queries files. If I do this and get it working I’ll post my code here, just don’t hold your breath!

  • I have tried to do some debugging and you are correct. The value is set but when rendering the field to show already selected posts ACF calls get_psots() with the following arguments

    
    Array
    (
        [posts_per_page] => -1
        [post_type] => Array
            (
                [0] => tribe_events
            )
    
        [post_status] => any
        [update_post_meta_cache] => 
        [update_post_term_cache] => 
        [post__in] => Array
            (
                [0] => 6092
            )
    
    )
    

    and this returns no results. The only conclusion at this point that I can come to is that there is something in the events plugin that is interfering with this query.

  • You cannot do what you are attempting to do.

    The issues
    1) A post object fields stores the post ID of the post object selected
    2) When set to allow multiple selections it stores a serialized array of post IDs

    The first thing you need to do is know the post ID of the post object you want to filter by

    
    'meta_query' => array(
      array(
        'key' => 'POST OBJECT FIELD NAME',
        'value' => '"'.$post_id.'"'. // the post id that you are looking for
        'compare' => 'LIKE'
      )
    )
    
  • 
    add_filter('acf/load_value/name=post_object_repeater', 'prepopulate_post_object_with_all_posts', 20, 3);
    function prepopulate_post_object_with_all_posts($value, $post_id, $field) {
      if (!empty($value)) {
        // already has a value, do not overwrite
        return $value;
      }
      
      // do a query to get all posts of the post type
      $args = array(
        'post_type' => 'add-ons', // your post type
        'posts_per_page' => -1, // -1 get all
        'fields' => 'ids', // return only list of IDs
      );
      $query = new WP_Query($args);
      if (empty($query->posts)) {
        // no posts found
        return $value;
      }
      
      $value = $query->posts;
        
      return $value;
    }
    
  • In my research, I read that you can’t do a new WP_Quey using repeater fields so I created a custom post type using the same ACF fields. The problem is on my ‘Biology’ page for example, I have two tutors who teach biology and that is checked in the backend but on the front end only one tutor shows. I’m using this query to get tutors who are checked for biology

    $args = array(
                    'post_per_page'	=> 10,
                    'post_type'		=> 'pg-tutor',
    				'meta_query' => array(
                        array(
                            'key'     => 'subject',
                            'value'   => 'biology',
                            'compare' => 'LIKE',
                        ),
                    )
                    );

    Any suggestions are appreciated.

  • BlakeHampson,

    You are right at naming those good points about Gutenberg.

    To me, this discussion boils down to what one wants to do with WordPress. If one is planning on creating a site with a scope that is similar to what one could do with SquareSpace or Wix, then Gutenberg is perfect.

    However, if one is planning on using WordPress as a CMS, managing a complex tree structure, with an intricate layout, where every bit of information must be available through WordPress queries from anywhere, in or outside the site, as discrete bits of information; and where, sometimes, one needs to import from an old site hundreds of pages, and precisely direct each old content type onto specific places in the backend (and the database), then Gutenburg is not your friend.

    It is not WordPress fault, nor Gutenberg’s. Many of us starting using WordPress for something it wasn’t built for. That’s how ACF came into the picture. It solves the need for a feature rich CMS.

    This debate has prompted me to look for WordPress alternatives. And in indeed, it is very difficult to find a CMS that is easy on the server, easy to update, with big community support, safe, user friendly, feature rich, longeve, and… that doesn’t cost an arm a leg on monthly fees.

  • Two advantages of Gutenburg that both of you seemed to miss:

    • Gutenburg loads faster than ACF, and will continue to load faster as it becomes more part of core
    • ACF (or any additional plugin) adds a dependency, increases the attack surface, and adds more requests than would be needed if you were just using something already in core

    These might be comparatively small advantages, but over time it seems they will become bigger (and may already have done since this conversation).

    I’d be interested to know what either of you would have to say about those points.

  • I see!! So I changed the code like below. It seems to work

    Thanks

    <?php if ( $site = get_field( 'vendor_fb' ) ) : ?>
        <?php
        $site = str_replace(array('http://', 'https://'), '', $site);
        $site = rtrim($site, '/');
        $url = get_field( 'vendor_fb' );?>
        <a class="vendor-link" href="<?php echo $url ?>" target="_blank"><?php echo $site ?></a>
    <?php endif; ?>
  • Here is my code. It return the correct name but not the url itself
    It looks like this on my site
    “facebook.com/abcphotography”
    href = my.site.com/facebook.com/abcphotography

    Does anyone know why the root url is there?
    Thanks

    <?php if ( $site = get_field( 'vendor_fb' ) ) : ?>
        <?php
        $site = str_replace(array('http://', 'https://'), '', $site);
        $site = rtrim($site, '/');?>
        <a class="vendor-link" href="<?php echo $site ?>" target="_blank"><?php echo $site ?></a>
    <?php endif; ?>
  • I’m using Contact 7 forms in this way. I have a flexible content field I’m using as a page builder.

    I added my shortcode to a text field schedule_a_demo_form like this;
    [contact-form-7 id="xxx" title="Schedule A Demo"]

    In my template I do this;
    $scheduleADemoForm = get_sub_field('schedule_a_demo_form');
    echo do_shortcode($scheduleADemoForm);

    Also, to prevent users from altering the shortcode in the text field I added a class admin-readonly to the wrapper and in my admin-styles.css added this style;

    .admin-readonly {
        pointer-events: none;
    }

    This prevents them from accessing the input (unless they know how to use developer tools that is 🙂 )

  • I’m running into the same problem but @ali_jafarian solution isn’t quite working perfectly for me. I have a CPT for artworks with custom taxonomy for artists. Then I have another CPT for artists. The artist pages are basically the the page title (artist name) and a relationship field where you add the artist’s artwork from the artworks CPT where you can also filter by the custom taxonomy (since there are 50+ artists and 100s of artworks).

    Each artwork is its own page as mentioned. When you click on the artwork from an artist page, we want to have the option of going to the next or previous artwork based on the order displayed on the artist page as determined by the order you set from its relationship field.

    There seems to be an issue with the following code. I’m note sure how $current_post_id applies to the array_search as it isn’t referencing anything anywhere else in the code. If I print_r $current_index nothing appears. Same goes for $prev_module and $next_module. However, if I print_r $module_ids the array appears as it should in the order I set on the Artist page in the Relationship field. $first_module and $last_module show the correct IDs too. I just can’t seem to get the current post ID to then determine next and previous.

    // create empty array for module ids
    $module_ids = array();
                 
    // loop through modules and add them to array
    foreach( $modules as $module ) :
        $module_ids[] = $module->ID;
    endforeach;
                 
    // get the current index
    $current_index = array_search( $current_post_id, $module_ids );
                 
    // find the prev/next items
    $prev_module = $current_index - 1;
    $next_module = $current_index + 1;
                 
    // find first and last modules
    $first_module = $module_ids[0];
    $last_module = end($module_ids);
Viewing 25 results - 5,426 through 5,450 (of 21,339 total)