1

Does apply_filters/do_action $tag name have a characters limit? Can be any problems if the tag name is way too long? I ask this because I know that WP transients have a limit of 45 characters.

FYI, this is the tag I'm referring to:

apply_filters( $tag, ....
1

I always thought that the memory size was the limit for the string length, but according to the PHP documentation:

string can be as large as up to 2GB (2147483647 bytes maximum)

There is also the maximum allowed memory size (memory_limit) for the PHP script, to consider.

Testing: Do not perform this on a production server:

You could always use the str_repeat idea from this answer to test it:

// Check the hook name size limit:

$long_hook_name = str_repeat( 'x', 1024 * 1024 ); // Adjust the size until you hit the roof!

add_action( $long_hook_name, function(){
    echo 'Greetings from the hook with a very long name';
});

do_action( $long_hook_name );

but only test this where it's safe (locally?) ;-)

The same should apply for the add_filter(), since add_action() is just a wrapper:

function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
            return add_filter($tag, $function_to_add, $priority, $accepted_args);
}

according to the source.

1

The $tag is used as a key in an array, so it is converted to either a string or an integer. There is no maximum size for strings, the available memory is the only real restriction. The size of an integer depends on the platform:

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18, except for Windows, which is always 32 bit. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.

Use strings for better readability. Don’t try to fill the memory. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.