Support » Plugin: Archive Control » Advanced Custom Fields

Viewing 6 replies - 1 through 6 (of 6 total)
  • SoLoGHoST

    (@sologhost)

    Yes, but you have to make edits to the CORE of this plugin currently, because I don’t see a way to manage this with filters/actions.

    Will need to add a custom rule for ACF also in your functions.php file

    class-archive-control.php file will also need edits to archive_control_settings and archive_control_edit_page_callback functions (which can be extended from another class also since it’s public).

    I created an ACF Rule called “Post Archive” like so (in my themes functions.php):

    
    add_filter('acf/location/rule_types', 'acf_location_rules_types');
    
    function acf_location_rules_types($choices) 
    {	
        $choices['Post']['archive'] = __('Post Archive', 'metro');
    
        return $choices; 
    }
    
    add_filter('acf/location/rule_values/archive', 'acf_location_rules_values_archive');
    
    function acf_location_rules_values_archive($choices)
    {
    	$args = array(
    		'public'   => true
    	);
    
        $post_types = get_post_types($args, 'names');
    
        if (!empty($post_types))
    	{
            foreach($post_types as $post_type)
                $choices[$post_type] = $post_type;
        }
    
        return $choices;
    }
    
    add_filter('acf/location/rule_match/archive', 'acf_location_rules_match_archive', 10, 3);
    function acf_location_rules_match_archive($match, $rule, $options)
    {
    	$post_type = !empty($_GET['post_type']) ? $_GET['post_type'] : '';
    	$page = !empty($_GET['page']) ? $_GET['page'] : '';
    
    	if (!empty($post_type) && !empty($page))
    	{
    		if ($rule['operator'] == '==')
    			$match = ($page == 'edit-' . $rule['param'] . '-' . $rule['value']) && $post_type == $rule['value'];
    		else if ($rule['operator'] == '!=')
    			$match = ($page != 'edit-' . $rule['param'] . '-' . $rule['value']);
    	}
        return $match;
    }

    Than all that’s left is to load the acf_form_head() and corresponding scripts needed for ACF as well as matching with the fields by using various different ACF filters in the class file: class-archive-control.php. You’ll need to provide an ACF post_id value that will have to be a string. ACF will automatically place this in the wp_options table, which you will need to get on the front-end.

    If you want exact code, let me know…

    • This reply was modified 1 year ago by  SoLoGHoST.
    Plugin Author thejester12

    (@thejester12)

    This is cool, I will look into adding this. I’m a big fan of ACF, and had wondered if this were possible, but I hadn’t made it a high priority because you can essentially make your own Options pages with ACF Pro, so I wasn’t sure how necessary this was. Thanks for the code SoLoGHoST, if you want you could consider making a pull request out here: https://github.com/TheJester12/archive-control

    SoLoGHoST

    (@sologhost)

    Hello thejester12, I’ll just give you the code changes that I made to the class-archive-control.php file. Currently, I didn’t bother with validation of ACF fields on this page as I didn’t see a need for it on the site I’m working on (that is using this awesome plugin). So, I would recommend calling the ACF validation filters to return TRUE/FALSE where $load_acf_form_head gets set (in the last conditional ifs). Anyways, here are the changes I’ve made to it. NOTE: I’ve tried loading up acf_form_head in the script function for this plugin, but it caused issues during saving of the page, so I moved it into the archive_control_settings function with extra condition checks before loading, otherwise (without these checks), it will cause js errors (undefined issues) when adding/editing Custom Fields in ACF.

    Ok, so here were my changes:

    archive_control_settings

    public function archive_control_settings()
    {
    	global $wpdb;
    
    	register_setting( 'archive-control-tax-options-group', 'archive_control_tax_category_options' );
    	register_setting( 'archive-control-tax-options-group', 'archive_control_tax_post_tag_options' );
    	$custom_post_types = $this->archive_control_get_cpts();
    	if (!empty($custom_post_types)) {
    
    		$load_acf_form_head = false;
    		if (isset($_GET['page'])) {
    			if (strpos($_GET['page'], 'edit-archive-') !== false) {
    				$load_acf_form_head = true;
    			}
    		}
    		if (isset($_GET['taxonomy'])) {
    			$load_acf_form_head = true;
    		}
    
    		foreach($custom_post_types as $post_type)
    		{
    			if (!empty($_POST['option_page']) && $_POST['option_page'] == 'archive-control-' . $post_type->name . '-group')
    			{
    				if (!empty($_POST['post_id']) && $_POST['post_id'] == 'archive_control_cpt_' . $post_type->name . '_acf')
    				{
    					// Delete all previous ACF options first, so they don't pile up and we get the correct values...
    					$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE 'archive_control_cpt_{$post_type->name}_acf_%' OR option_name LIKE '_archive_control_cpt_{$post_type->name}_acf_%'");
    
    					$load_acf_form_head = true;
    					break;
    				}
    			}
    		}
    
    		if (!empty($load_acf_form_head))
    			acf_form_head();
    
    		foreach($custom_post_types as $post_type) {
    			register_setting( 'archive-control-cpt-options-group', 'archive_control_cpt_' . $post_type->name . '_options' );
    			register_setting( 'archive-control-' . $post_type->name . '-group', 'archive_control_cpt_' . $post_type->name . '_title' );
    			register_setting( 'archive-control-' . $post_type->name . '-group', 'archive_control_cpt_' . $post_type->name . '_image' );
    			register_setting( 'archive-control-' . $post_type->name . '-group', 'archive_control_cpt_' . $post_type->name . '_before' );
    			register_setting( 'archive-control-' . $post_type->name . '-group', 'archive_control_cpt_' . $post_type->name . '_after' );
    		}
    	}
    	$custom_taxonomies = $this->archive_control_get_taxes();
    	if (!empty($custom_taxonomies)) {
    		foreach($custom_taxonomies as $taxonomy) {
    			register_setting( 'archive-control-tax-options-group', 'archive_control_tax_' . $taxonomy->name . '_options' );
    		}
    	}
    }
    

    archive_control_edit_page_callback

    public function archive_control_edit_page_callback()
    {
    	$screen = get_current_screen();
    	$current_post_type = $screen->post_type;
    	$options = get_option('archive_control_cpt_' . $current_post_type . '_options');
    	$archive_control_cpt_title = get_option('archive_control_cpt_' . $current_post_type . '_title');
    	$archive_control_cpt_image = get_option('archive_control_cpt_' . $current_post_type . '_image');
    	$archive_control_cpt_before = get_option('archive_control_cpt_' . $current_post_type . '_before');
    	$archive_control_cpt_after = get_option('archive_control_cpt_' . $current_post_type . '_after');
    	if ($screen->post_type == '' && $screen->parent_file == 'edit.php') {
    		$current_post_type = 'post';
    	}
    	$current_post_type_object = get_post_type_object($current_post_type);
    	$current_post_type_options = isset($options) ? $options : null;
    
    	do_action('acf/input/admin_head');
    	do_action('acf/input/admin_enqueue_scripts');
    
    	$acf_post_id = 'archive_control_cpt_' . $current_post_type . '_acf';
    
    	$acf_positions = array(
    		'acf_after_title' => array(),
    		'normal' => array(),
    		'side' => array()
    	);
    
    	?>
    	<div id="archive-control-edit-page" class="wrap">
    		<h1><?php printf(esc_html__( 'Edit %1$s Archive Page', 'archive-control' ),$current_post_type_object->label); ?></h1>
    		<?php settings_errors(); ?>
    		<form method="post" action="options.php">
    			<?php
    			settings_fields( 'archive-control-' . $current_post_type . '-group' );
    			do_settings_sections( 'archive-control-' . $current_post_type . '-group' );
    			?>
    			<div style="display:none">
    				<script type="text/javascript">
    					acf.o.post_id = "<?php echo $acf_post_id; ?>";
    				</script>
    				<input type="hidden" name="acf_nonce" value="<?php echo wp_create_nonce('input'); ?>" />
    				<input type="hidden" name="post_id" value="<?php echo $acf_post_id; ?>" />
    				<?php wp_editor('', 'acf_settings'); ?>
    			</div>
    			<div id="poststuff">
    				<?php 
    				$filter = array(
    					'archive' => $acf_post_id
    				);
    
    				$field_groups = apply_filters('acf/location/match_field_groups', array(), $filter);
    				$acfs = apply_filters('acf/get_field_groups', array());
    
    				if (is_array($acfs))
    				{
    					foreach($acfs as $acf)
    					{
    						if (!in_array($acf['id'], $field_groups)) continue;
    
    						$options = apply_filters('acf/field_group/get_options', array(), $acf['id']);
    
    						// load fields
    						$fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']);
    
    						switch($options['position'])
    						{
    							case 'acf_after_title':
    								ob_start();
    								echo '<div id="acf_' . $acf['id'] . '" class="postbox acf_postbox ' . $options['layout'] . '">';
    								echo '<h3 class="hndle"><span>' . $acf['title'] . '</span></h3>';
    								echo '<div class="inside">';
    													
    								do_action('acf/create_fields', $fields, $acf_post_id);
    								
    								echo '</div></div>';
    								$acf_positions['acf_after_title'][] = ob_get_contents();
    								ob_end_clean();
    							break;
    							case 'normal':
    								ob_start();
    								echo '<div id="acf_' . $acf['id'] . '" class="postbox acf_postbox ' . $options['layout'] . '">';
    								echo '<h3 class="hndle"><span>' . $acf['title'] . '</span></h3>';
    								echo '<div class="inside">';
    													
    								do_action('acf/create_fields', $fields, $acf_post_id);
    								
    								echo '</div></div>';
    								$acf_positions['normal'][] = ob_get_contents();
    								ob_end_clean();
    							break;
    							case 'side':
    								ob_start();
    								echo '<div id="acf_' . $acf['id'] . '" class="postbox acf_postbox ' . $options['layout'] . '">';
    								echo '<h3 class="hndle"><span>' . $acf['title'] . '</span></h3>';
    								echo '<div class="inside">';
    													
    								do_action('acf/create_fields', $fields, $acf_post_id);
    								
    								echo '</div></div>';
    								$acf_positions['side'][] = ob_get_contents();
    								ob_end_clean();
    							break;
    						}
    					}
    				}
    
    				?>
    				<div id="post-body" class="metabox-holder columns-2">
    					<div id="postbox-container-1" class="postbox-container">
    						<div id="submitdiv" class="postbox ">
    							<h2 class="hndle"><span><?php _e('Publish', 'archive-control'); ?></span></h2>
    							<div class="inside">
    								<div id="major-publishing-actions">
    									<a href="<?php echo get_post_type_archive_link( $current_post_type ); ?>" class="button" id="archive-control-view-archive"><?php _e('View Archive', 'archive-control'); ?></a>
    									<div id="publishing-action">
    										<?php submit_button(); ?>
    									</div><!-- #publishing-action -->
    									<div class="clear"></div>
    								</div><!-- #major-publishing-actions -->
    							</div><!-- .inside -->
    						</div><!-- #submitdiv -->
    						<?php $image_val = isset($current_post_type_options['image']) ? $current_post_type_options['image'] : null; ?>
    						<?php if ($image_val == 'enabled') : ?>
    							<div id="featured-image-archive" class="postbox ">
    								<h2 class="hndle"><span><?php _e('Archive Featured Image', 'archive-control'); ?></span></h2>
    								<div class="inside">
    									<?php
    									$upload_link = esc_url( get_upload_iframe_src( 'image' ) );
    									$featured_img_src = wp_get_attachment_image_src( $archive_control_cpt_image, 'full' );
    									$featured_img = is_array( $featured_img_src );
    									?>
    									<div class="featured-image-archive-container">
    									    <?php if ( $featured_img ) : ?>
    									        <img src="<?php echo $featured_img_src[0] ?>" alt="" style="max-width:100%;" />
    									    <?php endif; ?>
    									</div>
    									<p class="hide-if-no-js">
    									    <a class="upload-featured-image-archive-img <?php if ( $featured_img  ) { echo 'hidden'; } ?>"
    									       href="<?php echo $upload_link ?>">
    									        <?php _e('Set featured image', 'archive-control') ?>
    									    </a>
    									    <a class="delete-featured-image-archive-img <?php if ( ! $featured_img  ) { echo 'hidden'; } ?>"
    									      href="#">
    									        <?php _e('Remove featured image', 'archive-control') ?>
    									    </a>
    									</p>
    									<input class="featured-image-id" name="archive_control_cpt_<?php echo esc_attr($current_post_type); ?>_image" type="hidden" value="<?php echo esc_attr( $archive_control_cpt_image ); ?>" />
    								</div><!-- .inside -->
    							</div><!-- #featured-image-archive -->
    						<?php endif; ?>
    						<?php  if (!empty($acf_positions['side']))
    							echo implode('', $acf_positions['side']); ?>
    					</div><!-- .postbox-container -->
    					<div id="postbox-container-2" class="postbox-container">
    						<?php if ($current_post_type_options['title'] == 'custom') : ?>
    							<div id="titlediv">
    								<div id="titlewrap">
    									<label class="screen-reader-text" id="title-prompt-text" for="title"><?php _e('Enter archive title here', 'archive-control'); ?></label>
    									<input type="text" name="archive_control_cpt_<?php echo esc_attr($current_post_type); ?>_title" size="30" value="<?php echo esc_attr($archive_control_cpt_title); ?>" id="title" spellcheck="true" autocomplete="off">
    								</div>
    							</div>
    						<?php endif; ?>
    						<?php  if (!empty($acf_positions['acf_after_title']))
    							echo implode('', $acf_positions['acf_after_title']); ?>
    						<?php if ($current_post_type_options['before'] == 'textarea') : ?>
    							<div id="archive-control-before">
    								<?php /* <h2><span><?php _e('Before Archive List', 'archive-control'); ?></span></h2> */ ?>
    								<div class="inside">
    									<?php $settings = array(
    										'textarea_name' => 'archive_control_cpt_' . $current_post_type . '_before',
    										'textarea_rows' => 10
    									);?>
    									<?php wp_editor( $archive_control_cpt_before, 'before-archive', $settings ); ?>
    								</div><!-- .inside -->
    							</div><!-- #archive-control-before -->
    						<?php endif; ?>
    						<?php  if (!empty($acf_positions['normal']))
    							echo implode('', $acf_positions['normal']); ?>
    						<?php if ($current_post_type_options['after'] == 'textarea') : ?>
    							<div id="archive-control-after">
    								<h2><span><?php _e('After Archive List', 'archive-control'); ?></span></h2>
    								<div class="inside">
    									<?php $settings = array(
    										'textarea_name' => 'archive_control_cpt_' . $current_post_type . '_after',
    										'textarea_rows' => 10
    									);?>
    									<?php wp_editor( $archive_control_cpt_after, 'after-archive', $settings ); ?>
    								</div><!-- .inside -->
    							</div><!-- #archive-control-before -->
    						<?php endif; ?>
    					</div><!-- .postbox-container -->
    				</div><!-- #post-body -->
    				<br class="clear">
    			</div><!-- #poststuff -->
    		</form>
        </div>
    	<?php
    }

    This can probably be done cleaner, but this works for what I need. Also, I’d recommend adding a filter for the Titles above the wp_editor output, for “Before Archive List” and “After Archive List”. I didn’t need the Before Archive List text there, so I removed this bit.

    Again, like I said, no validation is currently happening for the ACF fields on these pages, but the ACF Fields are loading, saving, and being saved into options table nicely.

    As a BONUS, I created a get_archive_option function for this like so:

    function get_archive_option($field_name)
    {
    	global $wp_query;
    
    	$return = false;
    
    	$archive_post_type = $wp_query->get('post_type');
    
    	if (!empty($archive_post_type) && $wp_query->is_archive())
    		$return = get_option('archive_control_cpt_' . $archive_post_type . '_acf_' . $field_name, false);
    	
    	return $return;
    }

    Thanks again for an AMAZING PLUGIN which allows Content to be managed on Archive Pages (seems like something WordPress forgot to add)! Hope to see ACF Fields implemented on this soon. I don’t do pulls/pushes, because it seems too complicated for me with the WordPress way of doing it, as I’m more used to GIT. But if I can help in any other way, just shoot me an email at: solomon@thinkitfirst.com and I’d be glad to help! 🙂

    • This reply was modified 1 year ago by  SoLoGHoST. Reason: Forgot to add acf_after_title and normal in the callback function, fixed now!
    SoLoGHoST

    (@sologhost)

    I just realized that is a git url you posted, for the pull request. I do a lot of coding, and am on a deadline at the moment, but will be glad to help out on git when I get the chance, which isn’t going to happen very soon unfortunately 🙁

    Also, in the code above, I forgot to add in the acf_after_title and normal parts for ACF in the callback function, which are as follows:

    <?php  if (!empty($acf_positions['acf_after_title']))
    	echo implode('', $acf_positions['acf_after_title']); ?>
    <?php  if (!empty($acf_positions['normal']))
    	echo implode('', $acf_positions['normal']); ?>

    They just need to be placed in the areas where they go in the callback poststuff div id.

    Cheers 🙂

    • This reply was modified 1 year ago by  SoLoGHoST.
    SoLoGHoST

    (@sologhost)

    ACF option pages are a joke when it comes to what this plugin does. This plugin has the right idea to put it where it belongs. In the Post Type Submenus. Anyways, thanks again!

    Webaxones

    (@webaxones)

    @sologhost : Your work seems very interesting but I can’t understand how you link custom group fields to the archive page. I don’t see anything in Custom Fields Plugin that talk to me…
    Thanks for any advice

    EDIT : Sorry, I didn’t see the first part of code !
    And it works! Thanks & Merry Christmas.

    • This reply was modified 11 months ago by  Webaxones.
    • This reply was modified 11 months ago by  Webaxones.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Advanced Custom Fields’ is closed to new replies.