WordPress How To Switch Theme Dynamically by URL

I want to switch my WordPress theme dynamically where if my visitor visits a specific URL they will see different theme while my main theme is still the same for other URLs.

All Other Pages will Show Main Theme Like Below

wordpress switch theme dynamically - original theme
wordpress switch theme dynamically – original theme

 

Only for Page wordpress-how-to-switch-theme-dynamically-by-url/ will show below Theme

wordpress switch theme dynamically - different theme based on url
wordpress switch theme dynamically – different theme based on url

 

WordPress Hook to Change the Theme

You can do it by using pre_option_{$option} hook. In this case:

This hook will filter the value of an existing option before it is retrieved.

You can refer the pre_option hook here.

The Code to Change WordPress Theme Dynamically

Don’t Put The Code at Your Theme files

It doesn’t work if you put the code at functions.php or in any files under your theme folder.

You must put at a plugin.

Side Effect by Switching WordPress Theme Dynamically

By switching the theme dynamically, I noticed that:-

  1.  The my top main menu doesn’t appear. I don’t how to solve this yet.
  2. Google AdSense also seems not loaded
wordpress switch theme dynamically - top menu not appear
wordpress switch theme dynamically – top menu not appear

WordPress How To Change Page Title Programmatically

If you want to change your page title dynamically which change the title tag <title>, check whether your theme has this code under functions.php

If yes, if you want to change the page title dynamically, you must call below filter.

Check that your theme header.php doesn’t contain any title tag. It only contains wp_head()

 

I spent so much time finding this solution, maybe I didn’t typed the right keywords in search engine.

WordPress media_handle_sideload error message of Specified file failed upload test.

I got error “Specified file failed upload test.” when I try to upload image file using media_handle_sideload(). It took me several hours just to solve this. Haha

My HTML Form

My PHP

 

The error is not really verbose, it should say empty file found rather then failed test. My mistake I forgot to put the index ‘filename’ as in my form.

Solution

 

WordPress wp_title Hook Filter Not Working

When I use wp_title filter hook, the title on the browser doesn’t change. To solve this ensure at your theme header.php, there is wp_title() at the title tag.

Then you can use the wp_title filter.

WordPress How To Get Variable from Rewrite Rules

I was scratching my head on how to get the variable from WordPress rewrite rules. In one of Stackoverflow post, it was mentioned we could use $_GET and in some posts proposed to use get_query_var() but when I tried both were emptied.

The Correct Way To get Get Variable from Rewrite Rules

But some conditions must be fulfilled. Please read on.

If Using get_query_var is still Empty means you use non standard variable name

Here is the list of standard query vars that you can use.

Register Your Variable Name

If you use your custom variable, you must add the variable as below.

Ensure Your Regex Has ^ at the beginning and $ at the end

If not, WordPress standard rewrite rules will be executed first and you can ‘t get your variable value.

The Full Code

Make sure you press save at Settings -> Permalinks.

WordPress Error Establishing A Database Connection

Suddenly this morning around 11AM, 16th August 2021, my awesomegrasp.com is not reachable with error message of error establishing a database connection.

wordpress error establishing a database connection
wordpress error establishing a database connection
  • From cPanel, I check my database is still up and running.
  • Database username and password is correct.
  • My other website is still running (means my hosting is up and running as it i shared hosting).

Error Explanation at wp-admin

I go to wp-admin and the page said one of the tables is broken and to fix it I need to enable below code at wp-configs.php:

wordpress error establishing a database connection - wp allow repair
wordpress error establishing a database connection – wp allow repair

Run Repair Tables

After enabled WP_ALLOW_REPAIR, I run repair tables.

wordpress error establishing a database connection - repair database
wordpress error establishing a database connection – repair database

WordPress List Down The Repair Status

wordpress error establishing a database connection - database repair results
wordpress error establishing a database connection – database repair results

Remove WP_ALLOW_REPAIR

After remove WP_ALLOW_REPAIR, my website is up again.

My worry whether this is possible hacks in my wordpress installation.

NodeJS Reading and Writing to WordPress PHP via Rest API

I developed NodeJS program that can read and write to WordPress Rest API. I’m having problem of reading or writing properly the data in NodeJS and WordPress and vice versa.

NodeJS Writes JSON Objects to WordPress Rest API

Any JSON String[] or Object[], will be saved into serialized PHP string by WordPress.

Surprisingly, when NodeJS read the data save in serialize string, NodeJS can read it as JSON object without needs to call JSON.parse().

However JSON Object, will be saved as string by WordPress but still follows JSON string format.

As the JSON Object is saved as string, when NodeJS read the data via WordPress Rest API, NodeJS has to call JSON.parse() to convert it back into JSON Object.

Examples of JSON Object saved by WordPress Rest API

How To Have Consistency?

By having JSON arrays save as PHP serialize string and JSON object as string in PHP, I need to have do extra checking or conversion by calling JSON.parse()

Another possible solution is to call JSON.stringify() to JSON arrays so it is saved as string at PHP. However, it is still extra step that we need to do and it doesn’t solve the problem stated above where there is no consistency.

So, in short there is no possible solution to have consistency, we still need to do extra step as mentioned above.