dimanche 24 juillet 2016

Prettify my Wordpress ACF+Pro If/elseif endif statements

I want to share my PHP script that I wrote using On/Off values at the global and page-specific level.

Overall the script works GREAT so no issues with it working correctly

. I just wanted to share it and see if anyone would structure it differently or better.

Basically I am looking to "prettify" the statements. Not sure if they can be made faster or not cause it seems to already be really fast but optimizing it would be great.

Things to consider about this if/elseif statement:

  1. There is ACF Global Option value of: ON/OFF
  2. There is ACF Page Specific Option value of: ON/OFF
  3. IF no value at global or at page level exist then it is assumed to be ON and will show my desired code. I implemented this because I have over 399 sites on my WPMU installation and didn't feel like going into each of them and turning it ON. Figured I would do that on a case by case basis and seems to work great.

Here is the code:

<?php
/**
 * The Page List System for USLegal
 *
 * This system uses @wp_list_pages function: http://ift.tt/1KLw19z
 * This system has been enhanced by the pagelist plugin: http://ift.tt/1y0ahPc
 * [pagelist] - hierarchical tree of all pages on site (useful to show sitemap of the site);
 * [subpages] - hierarchical tree of subpages to the current page;
 * [siblings] - hierarchical tree of sibling pages to the current page;
 * [pagelist_ext] - list of pages with featured image and with excerpt;
 *
 * @package WordPress
 * @subpackage USLegal
 * @since USLegal 1.0
 */
?>

<?php if( get_field('page_list_global_onoff', 'option') ) :
/**
 *
 * Does the Page List system have a value, if so run if/else to determine the global value of On/Off. If On check to see if  * at the Page Settings level if it is On/Off. If neither has a value then by default show the Page List.
 *
 */
?>

    <?php if (get_field('page_list_global_onoff', 'option') == 'on') :
/**
 *
 * Page List global value equals ON, show Page List but check first to make sure it is not disabled on a specific page
 * Default to yes if no value exist at the Page level.
 *
 */
    ?>

        <?php if (get_field('pagelist_page_onoff') ) :
/**
 *
 * Does a value exist for Page List on the Page Specific Level. If not skip on/off and just show Page List
 * if turned on globally.
 *
 */
        ?>

        <?php if (get_field('pagelist_page_onoff') == 'on') :
/**
 *
 * Field values exist for PageList. The global value is either defaulted to yes or yes has been enbabled and at the Page
 * Level the Page has been set to ON. Let's show the Page List.
 *
 */
        ?>

<?php include get_template_directory() . '/inc/pagelist-output.php'; ?>

        <?php elseif (get_field('pagelist_page_onoff') == 'off') :
/**
 *
 * Page Level Field value is set to off. If global setting is ON but Page level value is OFF then nothing will be shown.
 * Placeholder for OFF
 *
 */
        ?>

        <?php endif; ?>

        <?php else : ?>

<?php include get_template_directory() . '/inc/pagelist-output.php'; ?>

        <?php endif; ?>

    <?php elseif (get_field('page_list_global_onoff', 'option') == 'off') :
/**
 *
 * Page List is turned off globally. Nothing will show at all across the site. Can't override this at the page level. If you turn it off globally it is assumed
 * that you are not interested in using it at all for this site. However if value is ON you then control what pages you want it on and what pages you don't. We
 * see this as a better way to control where you want it and where you don't.
 *
 */
    ?>

<?php endif; ?>

        <?php elseif (get_field('pagelist_page_onoff') ) :
/**
 *
 * Does a value exist for Page List on the Page Specific Level. If not skip on/off and just show Page List
 * if turned on globally.
 *
 */
        ?>

        <?php if (get_field('pagelist_page_onoff') == 'on') :
/**
 *
 * Field values exist for PageList. The global value is either defaulted to yes or yes has been enbabled and at the Page
 * Level the Page has been set to ON. Let's show the Page List.
 *
 */
        ?>

<?php include get_template_directory() . '/inc/pagelist-output.php'; ?>

        <?php elseif (get_field('pagelist_page_onoff') == 'off') :
/**
 *
 * Page Level Field value is set to off. If global setting is ON but Page level value is OFF then nothing will be shown.
 * Placeholder for OFF
 *
 */
        ?>

        <?php endif; ?>

        <?php else : ?>

<?php include get_template_directory() . '/inc/pagelist-output.php'; ?>

        <?php endif; ?>


 <?php if (get_field('pagelist_page_onoff') == 'onoverride') :
 /**
 *
 * A Page Specific ON - Override that will override the global OFF Page List when needed. Separated from main if/else for
 * the purposes of quicker loading instead of having to call the global on/off value it skips that and looks directly at
 * the page value value. if it is ON - Override then it will show.
 *
 */
 ?>

<?php include get_template_directory() . '/inc/pagelist-output.php'; ?>


 <?php else : ?>


 <?php endif; ?>

===============>>

Thank you for taking the time to review this and assist me. Just trying to make my code more pretty as I code more and more.

Aucun commentaire:

Enregistrer un commentaire