Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

Disabling shortcodes

Open dk-wordpress opened this issue 10 years ago • 12 comments

Hello!

I'm trying to add an option to disable shortcodes using admin panel, here is my code from /extensions/shortcodes/hooks.php:

function _filter_disable_unyson_shortcodes( $to_disable ) {
	$to_disable[] = 'call_to_action';
	$to_disable[] = 'icon';
	$to_disable[] = 'icon_box';
	$to_disable[] = 'team_member';
	$to_disable[] = 'notification';
	$to_disable = array_merge( $to_disable, fw_get_db_settings_option( 'disabled_unyson_shortcodes' ) );	
	return $to_disable;
}

add_filter( 'fw_ext_shortcodes_disable_shortcodes', '_filter_disable_unyson_shortcodes' );

this code causes an error: Undefined option type: table in admin panel, but if i remove this string, all works fine:

$to_disable = array_merge( $to_disable, fw_get_db_settings_option( 'disabled_unyson_shortcodes' ) );

it seems it is impossible to use fw_get_db_settings_option function in disable shortcodes hook?

Thank you in advance

dk-wordpress avatar Nov 25 '16 14:11 dk-wordpress

Please add fw_print($to_disable); before return $to_disable; and provide the screenshot (or the output code).

ghost avatar Nov 28 '16 13:11 ghost

Hi, MoldCraft!

Please look at screenshots, the first is a result of following code:

	//$to_disable[] = 'calendar';
	$to_disable[] = 'call_to_action';
	$to_disable[] = 'icon';
	$to_disable[] = 'icon_box';
	$to_disable[] = 'team_member';
	$to_disable[] = 'notification';
	
	fw_print($to_disable);
		
	return $to_disable;

the second:

	//$to_disable[] = 'calendar';
	$to_disable[] = 'call_to_action';
	$to_disable[] = 'icon';
	$to_disable[] = 'icon_box';
	$to_disable[] = 'team_member';
	$to_disable[] = 'notification';
		
	$to_disable = array_merge( $to_disable, fw_get_db_settings_option( 'disabled_unyson_shortcodes' ) );
	
	fw_print($to_disable);
		
	return $to_disable;

2016-11-28_15-34-48 2016-11-28_15-35-37

dk-wordpress avatar Nov 28 '16 13:11 dk-wordpress

  1. Do you use option-type table in Theme Settings?
  2. It will be faster if I will debug the code. Can you send me your admin and ftp credentials to moldcraft[at]email.com?

ghost avatar Nov 28 '16 15:11 ghost

No, i don't use Table option type in theme settings, i have table shortcode overridden only, but if i remove it from framework-customizations directory, the problem remains.

I develop website locally, so sorry, i can't get FTP access

dk-wordpress avatar Nov 28 '16 16:11 dk-wordpress

  1. This happens with default WP theme?
  2. Can you zip your theme and send me the download link to moldcraft[at]email.com?

ghost avatar Nov 28 '16 17:11 ghost

Sorry for a long-time response, it is possible that we work in different time zones :)

I have uploaded test theme with a minimal code to reproduce, please download it here

How to reproduce an error:

  1. Activate "Test" theme
  2. Go to Appearance > Theme settings and disable "Widget area" shortcode
  3. Go to Pages screen and try to edit any page or create new one

2016-11-29_10-45-53

dk-wordpress avatar Nov 29 '16 08:11 dk-wordpress

@moldcraft could you take a look at my previous message please

dk-wordpress avatar Nov 30 '16 15:11 dk-wordpress

  1. Here shortcodes are not loaded yet.
  2. In filter you call fw_get_db_settings_option() which requires option-types and option-types are initialized.
  3. Here the shortcodes are loaded (the table shortcode too). But this action in table shortcode will never be executed because option-types were already loaded.
  4. table shortcode has in options table option-type which causes the warning.

Thinking a solution...

ghost avatar Dec 01 '16 13:12 ghost

@moldcraft thank you for looking! Maybe it will be better to use get_option instead of fw_get_db_settings_option

dk-wordpress avatar Dec 01 '16 14:12 dk-wordpress

That definitely will work. I don't have any idea at the moment how to fix this

ghost avatar Dec 01 '16 21:12 ghost

Thank you for your time, @moldcraft

If someone else needs a solution:

Theme options code:

'disabled_shortcodes' => array(
	'type'  => 'multi-select',
	'value' => array(),
	'label' => __('Disable Shortcodes', 'textdomain'),
	'desc'  => __('Check shortcodes to disable', 'textdomain'),
	'population' => 'array',
	'source' => '',
	'prepopulate' => false,
	'choices' => array( 
		'widget-area' => __('Widget area', 'textdomain')
	),
  'fw-storage' => array(
    'type' => 'wp-option',
    'wp-option' => 'disabled_shortcodes',
  ),
),

shortcodes hooks.php

function _filter_disable_unyson_shortcodes( $to_disable ) {
	
	//$to_disable[] = 'calendar';
	$to_disable[] = 'call_to_action';
	$to_disable[] = 'icon';
	$to_disable[] = 'icon_box';
	$to_disable[] = 'team_member';
	$to_disable[] = 'notification';
		
	$to_disable = array_merge( $to_disable, get_option('disabled_shortcodes') );		
	return $to_disable;
}

add_filter( 'fw_ext_shortcodes_disable_shortcodes', '_filter_disable_unyson_shortcodes' );

dk-wordpress avatar Dec 02 '16 10:12 dk-wordpress

Nice!

One note: The option name is very general disabled_shortcodes and it can conflict with some plugin. Better prefix it with your theme slug, so it will affect only your theme:

fw()->theme->manifest->get_id() .':disabled_shortcodes'

or

get_template() .':disabled_shortcodes'

ghost avatar Dec 02 '16 12:12 ghost