adding new custom conditional logic
-
Hi @themifyme,
I’m not sure if modifying this plugin is allowed, but I was looking at the codes of different plugins and comparing them to figure out which best suited my requirements.
Here is what I found that would improve this plugin by a lot and I hope you could include this as a “new” feature in your plugin somehow:
- There are 3 parts (2 required, 1 optional) to modify in
init.php
order to “add” new custom conditions. - (REQUIRED PART) In
public function check_visibility( $logic )
, look for “General” Conditions Tab:if( ( isset( $logic['general']['home'] ) && is_front_page()) || (isset( $logic['general']['404'] ) && is_404() ) || (isset( $logic['general']['page'] ) && is_page() && ! is_front_page() ) || (isset( $logic['general']['single'] ) && is_single() ) || ( isset( $logic['general']['search'] ) && is_search() ) || ( isset( $logic['general']['author'] ) && is_author() ) || ( isset( $logic['general']['category'] ) && is_category()) || ( isset($logic['general']['tag']) && is_tag() ) || ( isset($logic['general']['date']) && is_date() ) || ( isset($logic['general']['year']) && is_year()) || ( isset($logic['general']['month']) && is_month()) || (isset($logic['general']['day']) && is_day()) || ( is_singular() && isset( $logic['general'][$query_object->post_type] ) && $query_object->post_type !== 'page' && $query_object->post_type !== 'post' ) || ( is_tax() && isset( $logic['general'][$query_object->taxonomy] ) ) ) { return true; }
- Prepend your “new” custom condition before
( isset( $logic['general']['home'] ) && is_front_page())
, example:if( ( isset( $logic['general']['newConditionName'] ) && (newCondition) ) || ( isset( $logic['general']['home'] ) && is_front_page()) || ... ... ) { return true; }
Here,
( isset( $logic['general']['newConditionName'] ) && (newCondition) )
is the “new” custom condition where “newConditionName” is the name of the custom condition used in the codes (best to only use “a-zA-Z0-9” character with no spaces) and “newCondition” is the condition that returns TRUE or FALSE.
An example of “newCondition” isstrtok(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), "/") == 'sub-page-1'
which returns TRUE if the URL path starts with “sub-page-1” and FALSE otherwise. “https://example.com” returns FALSE, “https://example.com/main” returns FALSE, “https://example.com/sub-page-1” returns TRUE, “https://example.com/sub-page-1/page-1” returns TRUE, etc. - (OPTIONAL PART) In
public function get_visibility_options( $selected = array() )
, look for “General” tab name in “/* build the tab links */” section:
$output .= '<li><a href="#visibility-tab-general">' . __( 'General', 'themify-cm' ) . '</a></li>';
- Replace “General” in
__( 'General', 'themify-cm' )
with desired name, such as “Custom and General”, to indicate custom conditions are in this tab, example:
$output .= '<li><a href="#visibility-tab-general">' . __( 'Custom and General', 'themify-cm' ) . '</a></li>';
- (REQUIRED PART) In
public function get_visibility_options( $selected = array() )
, look for “General” checkboxes in “/* build the tab items */” section:$output .= '<div id="visibility-tab-general" class="themify-visibility-options clearfix">'; $checked = isset( $selected['general']['home'] ) ? checked( $selected['general']['home'], 'on', false ) : ''; $output .= '<label><input type="checkbox" name="general[home]" '. $checked .' /><span data-tooltip="'.get_home_url().'">' . __( 'Home page', 'themify-cm' ) . '</span></label>'; ... ...
- Prepend your “new” custom condition checkbox before
$checked = isset( $selected['general']['home'] ) ? checked( $selected['general']['home'], 'on', false ) : '';
, example:$output .= '<div id="visibility-tab-general" class="themify-visibility-options clearfix">'; $checked = isset( $selected['general']['newConditionName'] ) ? checked( $selected['general']['newConditionName'], 'on', false ) : ''; $output .= '<label><input type="checkbox" name="general[newConditionName]" '. $checked .' />' . __( 'New Condition Name', 'themify-cm' ) . '</label>'; $checked = isset( $selected['general']['home'] ) ? checked( $selected['general']['home'], 'on', false ) : ''; $output .= '<label><input type="checkbox" name="general[home]" '. $checked .' /><span data-tooltip="'.get_home_url().'">' . __( 'Home page', 'themify-cm' ) . '</span></label>'; ... ...
Here, `$checked = isset( $selected[‘general’][‘newConditionName’] ) ? checked( $selected[‘general’][‘newConditionName’], ‘on’, false ) : ”;
$output .= ‘<label><input type=”checkbox” name=”general[newConditionName]” ‘. $checked .’ />’ . __( ‘New Condition Name’, ‘themify-cm’ ) . ‘</label>’;` is the “new” custom condition checkbox where “newConditionName” is the name of the custom condition used in the codes (best to only use “a-zA-Z0-9” character with no spaces) and “New Condition Name” is the label name that is visible (treat as normal text with spaces).
Here is my question:
Rather than modifying the plugin codes directly, can some codes be used to add these custom conditions to function.php / plugin file, such as:add_filter('conditional_menus_conditions', 'new_custom_conditions'); function new_custom_conditions($conditions) { $conditions[] = array( // name used by codes 'name' => 'newCustomCondition',// unique ID for the rule // label visible to people 'label' => __( 'New Condition Name', 'themify-cm' ),// name of the rule // test condition return boolean 'condition' => function($item) { if (newCondition) {return true;} return false; } ); return $conditions; }
- There are 3 parts (2 required, 1 optional) to modify in
- The topic ‘adding new custom conditional logic’ is closed to new replies.