I need to assign the "active" theme via script. Anyone know the API call needed to do this? Also, how do I retrieve the current theme via script (PHP)?
Update current_theme option:
update_option('current_theme', '[theme name]');
To get the theme's name use:
$themes = get_themes();
In current Wordpress version 3.4.2 you need to update 3 options to switch to another theme(minihyper - in my case)
update_option('template', 'minihyper');
update_option('stylesheet', 'minihyper');
update_option('current_theme', 'Mini Hyper');
The first two options are key, the third really does nothing except maybe you can use this option somewhere in code to display current theme name.
Update:
Here is a true way:
<?php switch_theme( $template, $stylesheet ) ?>
Example with minihyper:
<?php switch_theme( 'minihyper', 'minihyper' ) ?>
Related
I'm new to PHP code and am trying to find a way to display 3 lines from the version.php file within WordPress without having to download the file; look into it and move on - this is what I have come up with so far, but does not seem to be working. I'm sure I'm doing something wrong here and would greatly appreciate some help.
<?php
$version = "wp-includes/version.php";
$all_lines = file($version);
echo $all_lines[16];
echo $all_lines[23];
echo $all_lines[37];
?>
To be able to see specific version numbers on your frontend its best to create a shortcode and use that shortcode to output.
Add the below code in functions.php file.
function bks_show_versions( $atts ) {
global $wpdb;
$wordpress = $GLOBALS['wp_version'];
$php_version = phpversion();
$mysql_version = $wpdb->db_server_info();
return sprintf("<pre> wordpress version : %s | php version: %s | mysql version : %s </pre>", $wordpress, $php_version, $mysql_version);
}
add_shortcode( 'show_versions', 'bks_show_versions' );
This basically will fetch all the values and print them on the screen.
<pre> wordpress version : 5.7 | php version: 7.4.16 | mysql version : 5.7.24 </pre>
Now, at whichever page you want it to print,edit that page and add [show_versions] on that page. Now view that page. You should be able to see.
WordPress Version
PHP Version
MySql Version
You can use this article to understand how to add shortcode to a page.
The shortcode you have to add is : [show_versions]
I have developed a wordpress plugin. In my plugin I manage all pages by get parameters like (http://example.com/client-portal/?page=dashboard) and it was working till wordpress version 5.4
But new version of wordpress version 5.5 in automatically redirect http://example.com/client-portal/?page=dashboard to http://example.com/client-portal/. Get parameter vanished automatically.
I have added shortcode by this way -
//page short code for user page
add_shortcode( 'ccgclient_portal', array($this,'ccgclient_portal_shortcode_func') );
This is my shortcode function -
function ccgclient_portal_shortcode_func()
{
ob_start();
include_once 'pages/user/index.php';
return ob_get_clean();
}
And catch get parameters by -
if(isset($_GET['page']) && $_GET['page'] == 'dashboard'){
include_once 'dashboard.php';
}
I don't know what's wrong with the new version of wordpress (5.5).
Please can you help me ?
Thanks in advance.
I believe your issue is with the 'page' key, this is a post type slug and it's creating a conflict with WP in this version. This is the same as configuring the permalink to work with '?post=98979' or a similar format.
My suggestion is to try and use a different get key and see what happens.
Let me know what you get.
I have the same issue with my plugin.
My problem was not using a new key. My client defined "page" here. It is about all the old links around in the world.
Im my case I solved it like this:
add_action( 'parse_request', 'ai_parse_request', 1);
and in
function ai_parse_request( $query ) {
unset( $query->query_vars['page']);
return $query;
}
I remove the "page" parameter from the $query to avoid the 301 redirect.
I have made this "workaround" configurable as the page parameter is used actually for pagination in the blog. In you case you should only apply this if e.g. the parameter is not a number to make sure you don't break pagination globally!
I too searched in all forums and even I post my question on the whmcs forum but no response. What i need is that i create an addon in modules and i want to change template (from six to five) in hooks of this addon. The aim is to change template for specific clients.
I already test to change the GET var but not working :
$_GET['systpl'] = 'five';
I also tested this but the css files don't load. It redirect me to home :
global $smarty;
$template = $smarty->getTemplateVars('template');
$template = 'six';
$smarty->assign('template', $template);
$template = $smarty->getTemplateVars('template');
Any suggestion please?
I have done this in one of my products - to get this to work, you have to pull the global $systpl variable:
global $systpl;
$systpl = $tpl;
$GLOBALS['_SESSION']['Template'] = $tpl;
$GLOBALS['CONFIG']['Template'] = $tpl;
Where $tpl is the template name you are looking to set, in your case 'five'. You have to also set the GLOBALS variables there so that the user session is maintained with that template and so that the system knows to use that template name when pulling from the config.
Hope that is of help.
In WHMCS to load another template folder for specified page, I did:
<?php
use WHMCS\Database\Capsule;
use WHMCS\View\Menu\Item as MenuItem;
define("CLIENTAREA", true);
// Set the template you want to use for the custom page BEFORE init.php is called
$GLOBALS['_REQUEST']['systpl'] = 'five';
require("init.php");
// WHATEVER YOU ARE DOING IN HERE
// Set the session back to the default template:
$GLOBALS['_SESSION']['Template'] = 'six';
$ca->output();
I have a wordpress blog. I created a db table which stores dictionary information and I want to publish this data from a URL . (For ex: "myblogaddress.com/mytest.php")
I have been researching for 2 days but nothing works I tried.
In my page; I use the php code shown in blow.
<?php
global $wpdb;
$words = $wpdb->get_results("SELECT * FROM $wpdb->words")
echo $words[0]->ENG;
?>
I wonder that;
- Which directory does my php page to be into ?
- What I need to do (other config, permission etc.) to do what I want.
Regards.
If you're loading it from a standalone PHP file (ie not from within your WordPress theme), you'll have to call wp-load.php to initialise the WordPress variables (including $wpdb). Have a look at this answer, including the comment about only needing wp-load.php.
I'd consider using a relative path (what that would be would depend on where you put your page relative to WordPress) rather than using $_SERVER['DOCUMENT_ROOT'];, but that's just a personal preference.
EDIT
Rereading after seeing your comment, I've just realised $wpdb->words probably won't exist. Try
$words = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "words")
instead. That'll generate the table name correctly as wp_words. Of course, you'll need to populate it the same way.
I'd like to use a custom post type archive as a site's front page, so that
http://the_site.com/
is a custom post type archive displayed according to my archive-{post-type}.php file.
Ideally I would like to alter the query using is_front_page() in my functions.php file. I tried the following, with a page called "Home" as my front page:
add_filter('pre_get_posts', 'my_get_posts');
function my_get_posts($query){
global $wp_the_query;
if(is_front_page()&&$wp_the_query===$query){
$query->set('post_type','album');
$query->set('posts_per_page',-1);
}
return $query;
}
but the front page is returning the content of "Home" and seems to be ignoring the custom query.
What am I doing wrong? Is there a better way, in general, of going about this?
Note: I did post this in WordPress Answers but that community is comparatively tiny.
Posting the final solution as an answer (Isaac placed it as a comment) just for anyone still looking.
Isaac was on the right track with adding a filter via functions.php. What he did wrong was call is_front_page(), which doesn't work yet because we're in pre_get_posts and the query hasn't been executed yet.
We do have the current page ID however. So we can still solve this, by looking into the WordPress option register for an option called page_on_front, which returns the ID of the page the user set as frontpage.
(For an overview of all WordPress options, just visit <yourwordpressinstallation>/wp-admin/options.php in your browser.)
Which makes for the following solution as suggested by Ijaas:
add_action("pre_get_posts", "custom_front_page");
function custom_front_page($wp_query) {
// Compare queried page ID to front page ID.
if(!is_admin() && $wp_query->get("page_id") == get_option("page_on_front")) {
// Set custom parameters (values based on Isaacs question).
$wp_query->set("post_type", "album");
$wp_query->set("posts_per_page", -1);
// WP_Query shouldn't actually fetch the page in our case.
$wp_query->set("page_id", "");
}
}
You might have to alter a few conditional tags to make sure all plugins still work, depending on how heavily the query gets altered.
Hope this helps someone.
Update: as noted below, add !is_admin() to the if-statement to make sure the function only runs on the frontend. If you only want this action to run for the initial query, you could also add the main query check $wp_query->is_main_query().
In response to Robberts solution:
answered May 6 '13 at 12:04
adding && !isAdmin() to the if function other wise it replaces the post type query for all admin pages as well.
Just in case anyone has issues with this.
edit:
also adding && $wp_query->is_main_query() to the if statement stops it affecting widgets and the menu
so total code I have
if($wp_query->get("page_id") == get_option("page_on_front") && !isAdmin() && $wp_query->is_main_query()) {}
In order to get that query to work, you're going to have to add that code to a page template, create a page, set your template as the template for the page you just created, and then set that page as the home page in Settings => Reading in the admin area.
By the way, the is_front_page() function only returns true if you're on a page that's been set as the home page from the admin menu.
The other option would be to modify index.php, but if you did that, is_front_page() would always return false. In that case, you'd want to use is_home() instead.
I hope that helps.
Isaac, you are correct, I didn't thoroughly read your question and I made the assumption that you were looking to do this the "easy" way.
Anyway, I put your code on my test site and, indeed, it didn't work. I looked at my SQL log and it turns out that your code produces this in the query wp_posts.post_status = 'private'.
So, I tried adding the line $query->set('post_status', 'public'); to your function, and it worked just fine.
In summary, try this:
add_filter('pre_get_posts', 'my_get_posts');
function my_get_posts($query){
global $wp_the_query;
if(is_front_page()&&$wp_the_query===$query){
$query->set('post_type','album');
$query->set('posts_per_page',-1);
$query->set('post_status', 'public');
}
return $query;
}