Including another MySQL class in Wordpress - php

I have a Wordpress installation, say in localhost/blog/ path.
I also have another custom CMS installed in the root folder like localhost/ .
I want to include my CMS' MySQL class into the blog's installation, so in the blog theme's header.php, i include like so:
<?php
global $site_config, $_db // $_db is my CMS' equivalent to $wpdb;
include_once("../includes/class.query.php");
?>
I am able to access my CMS' database in the Wordpress blog.
However, some variables in the Wordpress like the author of the article went missing. Calling functions like "get_the_author_meta('ID')", "the_post_thumbnail('thumbnail')" returns empty string. Also gallery posted in posts gone missing too. Other content like the post content are working fine.
The content of the 'class.query.php' above is rather simple. I have narrowed it down to these few lines:
function connect()
{
if ($this->mysql_link = #mysql_connect($this->dbhost, $this->dbuser, $this->dbpass))
{
if (!mysql_select_db($this->dbase)) // <---- this is the line that screwed up the integration
{
....
I wonder if someone knows what's going on here. Why only some Wordpress functions are affected?
How can i fix this?
Thanks in advance.

Related

Use of undefined constant ABSPATH

I am using WP for the first time. I'm just trying to create a very basic script to echo the user's id and am having all sorts of issues.
The code is this and is currently located in wp-content/plugins (i'm not really sure where these things should be):
<?php
require_once ABSPATH . '/wp-includes/pluggable.php';
$user = wp_get_current_user();
echo $user->get_site_id();
I'd had it without the require initially but I was getting a function not defined error for wp_get_current_user. Now I'm getting Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH'...
Is there some sort of predefined set of files that I need to include or some specific directory I need to be putting my scripts so that these variables and functions are in scope? My understanding was that these things are supposed to be global.
Did you try code like that:
add_action('init', 'some_function_name');
function some_function_name() {
$user = wp_get_current_user();
echo $user->get_site_id();
}
The WordPress comes with hooks (actions and filters) to let other developers modify either core parts of the WordPress or code from other plugins / themes.
The code I describe in my answer, it is running your code when the whole WordPress , all the Plugins and the theme are loaded, thus you should have by default the wp_get_current_user() function and you should not need to include manually the pluggable.php.
This seems like is going to solve your problem.
Read more on hooks here: https://developer.wordpress.org/plugins/hooks/.
Side note. Keep in mind that in order to run your custom code you should register a proper WordPress plugin and activate it. If you have made a php file in the plugins folder, and you loaded using PHP functions like require/include the plugin probably will not operate as you expect although the source code it could be perfect. To get more details on how to write your own plugin, you could read here: https://developer.wordpress.org/plugins/

Exclude a function in PHP (Wordpress)

I recently purchased a Wordpress theme and having some problems with importing a feature from another theme (from the same author).
The author of the themes helped me a lot with importing some stuff to the new theme. While both themes share many similarities, there's still some tweaking that needs to be done.
One of the issues I'm currently facing is with a PHP file I imported from the original theme to the new one. Both Wordpress themes work with minisites, but the author coded them differently for each Wordpress.
When I copy/paste boxes.php from the old theme to the new theme, I get this error message:
Fatal error: Call to undefined function it_get_minisite() in /home/vincevc73/domains/quirk.be/public_html/wp-content/themes/explicit/inc/boxes.php on line 13
The author told me I had to exclude this, but I have no idea how to do this. I read I had to put /*-*/ around the functions to exclude it, but I'm not sure where to put these marks exactly
The it_get_minisite function appears three times in the file
$minisite = it_get_minisite($post->ID);
if($minisite)
{
#override general theme options with minisite-specific options
$boxes_layout = $minisite->boxes_layout;
}
#get the current minisite $minisite = it_get_minisite($post->ID);
if($minisite)
{
#add post type to query args
if(it_targeted('boxes', $minisite))
$boxesargs['post_type'] = $minisite->id;
}
$minisite = it_get_minisite(get_the_ID(), true);
I think he meant that you should remove all the code which requires this function. But then you will also have to remove the code which requires "$minisite" too. But that's only a guess based on the information you posted.

Including plugin files into main plugin file properly

I've been having this issue for a while but keep just working around it an thought I'd finally get it solved.
I'm trying to include files into my main plugin document (the one that has the plugin title and version in it) like this:
define('SBT_PLUGIN_URL', plugin_dir_url(__FILE__));
include(SBT_PLUGIN_URL . 'competition_table.php');
inside the competition_table.php is an add_shortcode(); function that needs to run, in order for the shortcode to be registered with wordpress:
function add_table() {
//Run code here
}
add_shortcode('competition_table', 'add_table');
When I run the code on the site the link resolves properly, including the correct file, however I get this Fatal Error:
Call to undefined function add_shortcode()
However if I add exactly the same code that is in the competition_table.php into my main plugin document then the code runs perfectly.
So basically, my question is, why is Wordpress not recognizing it's own function and how can I include the file to make the code run properly?
Thanks in advance
You have to develop with WP_DEBUG enabled. It dumps an error: wrapper is disabled in the server configuration. That lead me to this: "Trust me, you do not want to include from URLs.".
Then I realized you're defining that constant with plugin_dir_url(), when what you need is a path. The following magic constant does the job:
include_once __DIR__ . '/competition_table.php';
Thanks to the feedback from #b__ I have managed to solve this issue.
For some reason, Magic Constants don't always work with wordpress, however, you can use it's equivalent to get the same effect:
include_once dirname(__FILE__) . '/competition_table.php';
When including files for use in a wordpress plugin you should always include via a PATH, not by a URL.

Wordpress cannot find function from plugin

I have installed a plugin wp-fb-autoconnect
I have activated it in the dasboard. Now I would to use a function from it.
In my theme folder, in one of my templates I want to add FB connect button
if( function_exists(‘jfb_output_facebook_btn’) )
{
jfb_output_facebook_btn();
jfb_output_facebook_init();
jfb_output_facebook_callback();
}
function_exists(‘jfb_output_facebook_btn’) returns false. The function is located in main.php in the plugin folder and i can see it. What's wrong? Please help.
UPDATE
The problem in general. A plugin has declared a function. I need to get access to that function from my theme folder.
‘jfb_output_facebook_btn’ uses formatted quotes. Did you copy/paste that snippet from a blog? Try:
if( function_exists('jfb_output_facebook_btn') )
{
jfb_output_facebook_btn();
jfb_output_facebook_init();
jfb_output_facebook_callback();
}
Be very careful of the kind of quotation marks you pass to PHP!
(BTW: Awesome user name. That game is brutal difficult.)

Wordpress loop inside of function = fatal error

I have my Wordpress blog on blog.mysite.com, and a totally different site (built in my framework) at www.mysite.com.
I know if the blog and another site are on the same server, and have correct permissions, I can use the following to "syndicate" my blogs to the non-blog site with:
define('WP_USE_THEMES', false);
require('/var/www/vhosts/mysite/subdomains/blog/httpdocs/wp-config.php');
query_posts('showposts=5');
...and then run a loop on the page.
The problem is that since my non-blog site is a framework, everything except for my front controller lives in a function - and this is creating the following error for me:
Fatal error: Call to undefined method
stdClass::set_prefix()
(btw, I'm fairly certain the fact that this code is called within a function that is causing the error...when I put the code on the front controller (not in function), the error disappears)
I would REALLY like to have recent blog listings on my non-blog site to spruce it up. Any ideas on how to do this?
Well I guess you forgot to write
global $wpdb
inside the function

Categories