Creating PHP Tests
PHP Tests let you run different server-side code for each variant of a test. They are intended for advanced use cases where the change you want to test cannot be implemented through page content, CSS, or JavaScript.
Because PHP Tests execute while WordPress is generating the page, they can modify backend behavior, WordPress queries, theme output, plugin functionality, and other server-side processes.
Important: PHP Tests require solid knowledge of PHP and the WordPress hook system. Incorrect code may cause errors or prevent pages from loading. Always validate your test before starting it on a live site.
When Should I Use a PHP Test?
Use a PHP Test when you need to:
- Modify server-generated content.
- Change WordPress behavior through actions or filters.
- Alter a query or template value before the page is rendered.
- Integrate a test with custom theme or plugin code.
- Create a test type that is not covered by Nelio A/B Testing’s built-in tests.
For changes that only affect the appearance or behavior of an existing page, a CSS and Text Test or JavaScript Test will usually be easier to create and maintain.
How PHP Tests Work
Each variant in a PHP Test contains a PHP code snippet. When WordPress processes a request, Nelio A/B Testing determines:
- Whether the request is included in the test scope.
- Which variant is assigned to the visitor.
- Which PHP snippet should be evaluated.
The selected snippet can then register WordPress actions and filters or execute other server-side logic.
Unlike JavaScript Tests, PHP Tests run on your WordPress server before the final page is sent to the visitor.
Use WordPress Actions and Filters
Most PHP variants should use WordPress hooks instead of executing all their logic immediately. For example, a variant that changes a post title could register a filter:
add_filter( 'the_title', function( $title ) {
if ( is_page( 123 ) ) {
return 'Alternative Page Title';
}
return $title;
} );Code language: PHP (php)Using hooks makes it easier to run your code at the correct point in the WordPress lifecycle and limits the impact of the variant.
Avoid Declaring Global Functions
Be careful when defining functions inside a PHP variant. If two snippets declare a function with the same name, or if the function already exists in a theme or plugin, WordPress may produce a fatal error.
Avoid generic function names such as:
function change_title() {
// ...
}Code language: JavaScript (javascript)Use a unique prefix that includes the test and variant identifiers instead:
function nab_123b_change_title( $title ) {
return 'Alternative Page Title';
}
add_filter( 'the_title', 'nab_123b_change_title' );Code language: PHP (php)A recommended naming format is:
nab_<test-id><variant>_<function-name>Code language: HTML, XML (xml)For example:
nab_123b_custom_titleAnonymous functions can also help avoid naming conflicts when the callback does not need to be removed later.
PHP Test Scope
The test scope determines which requests may participate in the test.
By default, Nelio evaluates the scope of a PHP Test during the plugins_loaded action. At that point, WordPress plugins have loaded, but much of the information about the current request may not yet be available.
For more advanced conditions, you can use a PHP-based scope and choose when Nelio should evaluate it.
The available evaluation points are:
plugins_loadedparse_querywp
Choose the earliest action that provides all the information your condition needs.
plugins_loaded
Use plugins_loaded when the scope does not depend on the current post, query, or page. This is the earliest available option and provides the best performance, but it offers the least information about the request.
parse_query
Use parse_query when you need access to the main WordPress query or its query variables. At this stage, WordPress has parsed the request, but some template-related functions may still be unavailable.
wp
Use wp when the condition depends on the current page, post, or queried object. For example, if your scope uses get_the_ID(), you should evaluate it during wp. This action provides more context, but because it runs later, some earlier WordPress hooks will no longer be available to your variant.
Execution Timing Matters
Nelio evaluates the PHP variant after confirming that the test applies to the current request. Your variant can only register callbacks for actions or filters that have not already executed.
When choosing the scope evaluation point, consider both:
- the information needed to determine whether the request belongs to the test; and
- the WordPress hooks your variant needs to use.
Example: Changing a Post Title
The following variant changes the title of a specific page:
add_filter( 'the_title', function( $title, $post_id ) {
if ( 123 !== $post_id ) {
return $title;
}
return 'Try Our New Alternative Headline';
}, 10, 2 );Code language: PHP (php)This type of test could also be implemented with a Page Test or CSS and Text Test. A PHP Test is more appropriate when the value must be changed on the server or reused by other WordPress functionality.
Example: Modifying the Main Query
The following example changes the number of posts displayed on an archive:
add_action( 'pre_get_posts', function( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_archive() ) {
$query->set( 'posts_per_page', 20 );
}
} );Code language: PHP (php)Because pre_get_posts runs before the wp action, the test must be evaluated early enough for this callback to be registered in time.
Example: Adding Content to a Page
The following variant appends a message to the content of a specific page:
add_filter( 'the_content', function( $content ) {
if ( ! is_page( 123 ) ) {
return $content;
}
return $content . '<p>Start your free trial today.</p>';
} );Code language: PHP (php)Always escape dynamic values before adding them to HTML output. The example uses fixed text for simplicity.
Best Practices
When creating PHP Tests:
- Keep each variant focused on the change being tested.
- Use WordPress actions and filters whenever possible.
- Use unique prefixes for named functions, classes, constants, and global variables.
- Avoid modifying unrelated requests.
- Check the current page, query, or content type before applying a change.
- Choose the appropriate scope evaluation point.
- Avoid slow database queries or expensive operations.
- Make sure every variant can run independently.
- Test compatibility with your theme, plugins, caching system, and WooCommerce configuration when applicable.
- Validate the test in a staging environment before starting it.
Security Considerations
PHP variants execute on your WordPress server and should be treated like custom plugin or theme code.
Follow standard WordPress security practices:
- Sanitize and validate input values.
- Escape dynamic output.
- Check user capabilities before performing restricted operations.
- Use nonces when processing form submissions or requests.
- Avoid executing code received directly from visitors.
- Do not expose private information in page output or tracking events.
Nelio A/B Testing does not make unsafe PHP code secure automatically.
Did you find this useful?
Thanks for your feedback!
Not what you’re looking for? Ask us directly:


