Loading Wordpress functions inside Zend Framework - php

Hio,
So I have a website which uses the Zend Framework as MVC (www.site.com), and a separate blog set up in wordpress (www.site.com/blog) on the same server, but I want to be able to use Wordpress functions on various pages to pull posts from wordpress.
Currently, the webservers DocumentRoot is /httpdocs/public. public contains a symlink to /httpdocs/blog (I have Options FollowSymlinks on)
All requests are routed through htaccess rules which either redirects it to httpdocs/public/index.php (which then loads Zend stuff) or a regular file/other area not inside the Zend application.
The example code in wordpress is to use the following:
define('WP_USE_THEMES', false);
require(APPLICATION_PATH .'/../blog/wp-blog-header.php');
But... this just tries to redirect me from site.com/page to site.com/page/wp-admin/install.php (which doesn't exist, and so I get a 'this page is redirecting in a way that will never complete' error from firefox, even though site.com/blog is setup and works perfectly fine. For some reason it doesn't seem to recognise that it is (probably because of path issues...)
Does anyone know how I could fix this??
Note: at the moment I just query the wordpress database, but this doesn't work properly because its not formatting the post content properly.

This is a known issue (Tested with WP 3.2.1 at the time of writing):
http://wordpress.org/support/topic/calling-wp-blog-headerphp-from-inside-a-php-function
The solution:
Avoid including the wordpress core inside any class method or function. If it still doesn't work, check the contents of debug_backtrace() to see if you really are on the global scope.
The explanation:
Wordpress sets some variables (direct assignment, does not use $GLOBALS) during initialization. If it's not initialized on the global scope, these variables belong to the current function or method scope. The result is that these variables cannot be accesed by wordpress's core functions, as they rely on the global keyword.

Benno,
I think I found a work around for this. azkotoki was right, it seems to be an issue of scope when including the wp-blog-header.php or wp-load.php files.
The headers have to be included at a global scope or else Wordpress gets confused and doesn't think its installed and redirects to wp-admin/install.php.
I made my require_once call right along with my Zend application require_once call in index.php of my Zend application's /public/ directory. Unfortunately, this means the Wordpress header will be included even when unnecessary, but at least its working.
...From /public/index.php...
/** Zend_Application */
require_once 'Zend/Application.php';
/** Wordpress Application */
require_once 'wordpress/wp-load.php';
Hope this helps, as it was driving me crazy!

Related

How to change a Wordpress core DB function without editing core files?

I have been fighting with this problem for several days now & I would appreciate input from (PHP / Wordpress) developers that are more experienced than me.
I'm making a debugging plugin for Wordpress, but one of the errors that I need to change comes from this file:
/site/public/wp-includes/wp-db.php
I need to change the structure and add more information to the $error_str in lines 1363 and 1366 so the output better suits our log reader and contains more useful information.
To do that I think I need to change the print_error() in line 1344
The problems are:
That the file is a core file in Wordpress, so I can't edit it directly.
It also have a global $wpdb variable that is used to call: $wpdb->query() & $wpdb->get_row() (also core functions in same file & class) that both in turn calls the print_error() function.
Other plugins are using these functions & I can't mess up their functionality but I should change their output too.
I think the most likely solution is to inherit & override the print_error() (and maybe also query() & get_row() so I can re-direct them to my new print_error()), but other plugins are using the $wpdb & those functions, I can (should) change the error_log() output for those plugins too, but I can't edit those plugins either & I can't mess them up with a faulty solution.
Questions:
How can I replace the print_error()?
What is the best way to do this?
How do I deal with the global $wpdb variable?
Do any of you have a better solution? I would very much appreciate it.
Link to the print_error() documentation (with source code):
https://developer.wordpress.org/reference/classes/wpdb/print_error/
Link directly to the source code: https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/wp-db.php#L1344
Not entirely sure this is an excellent idea, but you could extend the wpdb class and replace the global $wpdb object.
E.g.
global $wpdb;
$wpdb = new YourWpdbClass(); // you can override methods here to your heart's content
wp_set_wpdb_vars();
Officially, the way to replace the global $wpdb is by creating a db.php file inside wp-content. So if you plugin needs to alter db functionality it would need to include that file and possibly custom installation instructions

PHP Functions from included files don't execute on Web Server

I am in the process of migrating a site from my personal dev server onto Windstream's business hosting server. I've already run into the issue of having developed using PHP 5.4 only to find out that my static functions won't work on WS's 5.1.4 installation. I've since fixed those issues and am not facing one that I can't seem to find any help for on the internet.
All of the static functions I was using have been rewritten as functions outside the class scope. Instead of having
class Product{
...
public static function myFunction(){}
...
}
I now have
function myFunction(){}
class Product{...}
in my included Product.php file.
However, when I try to call myFunction() from my code, nothing happens. I know the nothingness comes from WS's error handling, but the point is, the function isn't working. To verify this, I have taken the following steps:
Inserted the line echo "entered included"; immediately following the <?php in Product.php. This prints "entered included" on the index page, indicating that my include is working. I have done the same thing before the final ?> with the same results, so I don't think it's getting hung up inside the included file.
I have changed myFunction() in the included file to be simply
function myFunction(){echo "myFunction works";}
A call to myFunction() still makes nothing happen.
I have moved myFunction() to the including file (myFunction() now lives in index.php instead of Product.php; index includes Product.php). This time, myFunction() executes without issue.
To my 'hack it til it does what it should' sensibilities, this tells me that the server is having a problem with functions that are declared in files that are included; honestly, though, I have absolutely no clue what's going on, and any help would be appreciated. Their website is currently down, and they were expecting it to only be offline for a day, so I'll try pretty much anything short of sacrificing a fatted calf.
I know I should be posting more specific code, but since this is a customer's actual website, I'm trying to put as little of the actual code out here as is possible. I'm happy to append specific sections of code to this entry as they are requested for clarification.
Thanks in advance for your consideration.
#Rottingham: First, thanks for the 3v4l link. Second, my assumption about static methods in 5.4 vs 5.1.4 came from this line of php.net's page on static members and methods:
"As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static)."
src - http://www.php.net/manual/en/language.oop5.static.php
Since my version and the server version were on different sides of the 5.3 mark mentioned, I incorrectly assumed that this was my problem.
Third, when I get in from my day job, I'll update my code to show errors and update this post if a solution has not yet been found.
Ultimately, my problem isn't with using static methods (since I don't have them anymore) but with using any function that is declared in an included .php file.

Joomla 3! Module Parameters

I have a Joomla website where I have a custom module with mod_myModuleName.php and mod_myModuleName.xml files and a folder where there are several PHP scripts that add special functionality to my module. There is a config.php file in the folder that holds an associative array with variables and their values hard-coded. The module works just fine.
What I want though is to provide administrator area for the values of the variables in the array, so that I can put values in administrator panel and get their values in config.php. In my mod_myModuleName.php I use <?php echo $params->get('param')?> and it works like a charm.
But when I try to use the same technique in the config.php it breaks my code. I tried to get the values in mod_myModuleName.php and then include it in config.php and use the variables but it does not work either. I have not got so much experience in php and cannot understand what can be the reason.
It sometimes gives me an error of something non object and I guess it must be something connected with object oriented php, am I right? And if so is there a way to overcome this without object orientation or how can I solve my problem?
The problem will be with the way you're using your config.php.
When your modules entry point file mod_myModuleName.php is loaded by Joomla the $params object is already available in that context, you need to provide it to your scripts.
If you look at something like the mod_articles_latest module you will notice that the helper class is included with this line:
require_once __DIR__ . '/helper.php';
And then helper class is has it's getList() method called statically with the $params passed into it, so that $params is available to class context:
$list = ModArticlesLatestHelper::getList($params);
Inside the helper class ModArticlesLatestHelper you will notice that the getList() expects the $params to be passed in.
public static function getList(&$params)
{
...
}
I would strongly recommend reading the articles in the Modules section of Developers Portal on the Joomla Doc's.
Try the "Creating a simple module" article.

CodeIgniter: Can't Get My New Controller/View To Show

I am learning how to use codeIgniter as my php framework. I am reading through the documentation and watching the intro video and just generally following along with the first tutorial, but it's not working for me.
I have created a controller called "test.php" and a view called "test_view". The controller for this class is exactly like "welcome.php" and the view file just has some static html. However, when I go to index.php/test I get a 404 error.
I have also tried manipulating the original welcome files so that instead of calling a view it just echos "testing", yet I still see the original welcome message! I've tried clearing my browsing cash and refreshing, but to no avail.
Any suggestions? Thanks.
Edit: Here's the code for controllers/test.php
<?php
class Test extends Controller {
//Just trying to get it to echo test
public function index()
{
echo "test";
//$this->load->view('test_view');
}
}
?>
Try looking at this page in the documentation - this might solve your problem.
This basically means you should try typing index.php?/test/ instead (notice the question-mark).
First of all, check the above link. Might be useful.
If not, then...
Try changing the default controller in the config file ('routes.php') to something else (probably, to 'test'), then try loading index.php. Just to test whether the whole system works (or not).
Check whether mod_rewrite is loaded (in your server .conf-file, if you're using Apache).
Try using the latest build of the framework. AFAIK, the name of the controller class for now is "CI_Controller".
Finally, try removing the word 'public' before the declaration of the function. AFAIR, CI enable you to make private functions in controllers just by using prefix (which is set in the config file) at the beginning of the name of the function (thus making all the other functions public).
But most certainly the problem is with the mod_rewrite. If not, try debugging with die('Page found'); instead of echo - this will allow you to track possible redirects on the page.

How to include a codeigniter project into another php file?

I have a project based on codeigniter. And I should use one class that extended from a codeigniter controller in another php file. But I didn't find the solution about how to teach another php file to see whole CI-project. Beyond that needed class can not inherit when i call it from other place.
I'm not 100% sure if this helps get you in the right direction, but kudos if it does!
Codeigniter routes the application depending on the environment state of the URI. What you need to do is set the environment and include the index view file like so:
$_SERVER["REQUEST_URI"] = "cms/2";
//Set GET action,method params etc
require_once "path/to/index.php";
When you load CI Index file it reads the SERVER Variable and others which you may have to find and execute the controller and method, I would also advise that you modify the library/view file as it may exit upon output causing your script to exit.
Also you may wis hto look into ob_start() to catch the buffer.

Categories