Tag

WordPress

WordPress

Debugging WordPress SQL Queries

There are various ways to investigate/debug issues with SQL statements within WordPress. I have outlined the most relevant methods below. Enable Debugging First, the debug mode needs to be enabled. This can be done by adding the following entries to the wp-config.php file: define( 'WP_DEBUG', true ); // Enable debugging define( 'WP_DEBUG_LOG', true ); // Write debug information to /wp-content/debug.log define( 'WP_DEBUG_DISPLAY', false ); // Do not display debug information NOTE: WP_DEBUG_DISPL

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

WordPress

Logging WordPress SQL Queries

To log the database queries executed by WordPress, you need to enable SAVEQUERIES in the wp-config.php file. Add the following entry: define( 'SAVEQUERIES', true ); Then, add the following code to a plugin or to the functions.php file of the active theme: add_action( 'shutdown', function () { global $wpdb; $log_stack = true; $log_file = fopen( ABSPATH . '/wp-content/sql.log', 'a' ); fwrite( $log_file, PHP_EOL . PHP_EOL . "##################################################

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

WordPress

Always show "Read more" when using GeneratePress

If you have the problem, that some posts do not show the “Read more” link when using WordPress in combination with the GeneratePress theme, you can use the following code snippets to solve your problem. The problem sometimes occur, when your full post contains some special tags, e.g. <pre> or ``. To solve the problem, you have to disable the default “Read more” link. This can be done by adding this code to your themes functions.php: add_filter( 'excerpt_more', function ( $more ) { return

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

WordPress

Hide and show widgets programmatically

To hide a specific widget on certain pages or under specific conditions, you can rely on a plugin like Widget Logic or Widget Options. Alternatively, you can achieve the same result with a few lines of code: add_filter('sidebars_widgets', 'pure_conditionally_remove_widget'); function pure_conditionally_remove_widget($sidebars_widgets) { if (is_admin()) { return $sidebars_widgets; } if (!is_front_page() && !is_archive()) { foreach ($sidebars_widgets as $widget_area => $widget_

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

WordPress

Get active WordPress plugins

The code below is an example of a function written in PHP, designed to list all active plugins on a WordPress website. This can be useful for gaining an overview of the plugins being used on a specific website. function sw_list_active_site_plugins() { $plugins = get_option( 'active_plugins' ); foreach ( $plugins as $key => $value ) { $string = explode( '/', $value ); echo $string[0] . "\n"; } } sw_list_active_site_plugins();

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

WordPress

Deactivating all WordPress plugins using an SQL statement

To deactivate all WordPress plugins using SQL (for example, if the backend becomes unusable due to a plugin), you can use the following statement: UPDATE wp_options SET option_value = '' WHERE option_name = 'active_plugins'; Note: If a different table prefix is set other than wp_ , the statement needs to be adjusted accordingly.

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

WordPress

Change Number of Search Results per Page

If you want to change the number of search results per page, you can use the following snippet: add_action( 'pre_get_posts', 'wpkb_change_number_of_search_results_per_page' ); function wpkb_change_number_of_search_results_per_page( $query ) { if ( is_search() && $query->is_main_query() ) { $query->set( 'posts_per_page', 100); } return $query; } If you want to avoid problems in the backend, add !is_admin() && to the if condition. Important notice: Yo

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

WordPress

Configure WordPress Revisions

WordPress by default keeps an unlimited number of revisions for pages and posts (and custom post types – if enabled). If you want to limit the number of revisions, you can add this line of code to your wp-config.php: define( 'WP_POST_REVISIONS', 5 ); // limits revisions to 5 If you want to completely disable revisions, you can add this line to your wp-config.php: define('WP_POST_REVISIONS', false );

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

WordPress

Configure WordPress Backend Search to Search Titles Only

By default, the WordPress backend search searches for post titles and the post content. If you have a lot of pages, it can be hard to find the correct page when you want to link pages or insert pages to a menu. If you want to change that, so that WordPress only searches for the post title (in the backend), you can use this code snippet: if ( is_admin() ) { add_filter( 'posts_search', 'search_by_title_only', 500, 2 ); } /** * @param $search * @param $wp_query * * @return string */ public fun

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

WordPress

Fix 404 errors when installing WordPress in a subfolder

If you use WordPress in combination with a Laravel Forge provisioned server, you may encounter problems when WordPress is not installed at the root level. I discovered especially REST errors (404), when the WordPress installation is contained in a subfolder. To fix that problem, add this to your nginx configuration: server { [...] location /my_subfolder { try_files $uri $uri/ /my_subfolder/index.php?$args /my_subfolder/index.php?q=$uri&$args; } [...] } Replace my_s

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer