I have wrote a PHP script that grabs the latest forum posts by categories that I want, which works fine and everything, however my problem lies in the actual output of the code itself, previously I was utilizing XenForo and could use their API on external pages no problem, now that we have migrated to IP.Board I am trying to utilize the IPSLib Library in order to use the parsing system so that the posts I have pulled don't show up all messy and with bbcode everywhere.
The code that pulls the information from IP.Board's database works fine, again I'm simply trying to load & use the IPSLib from IP.Board.
My question is how, if possible, can you load the IPSLib in order to utilize the functions provided by the library?
require_once( 'forums/initdata.php' );
require_once( IPS_ROOT_PATH.'/sources/base/ipsRegistry.php' );
require_once( IPS_ROOT_PATH.'/sources/base/ipsController.php' );
$registry = ipsRegistry::instance();
$registry->init();
$classToLoad = IPSLib::loadLibrary( 'forums/admin/sources/classes/text/parser.php', 'classes_text_parser' );
$parser = new $classToLoad();
$parser->set( array( 'parseArea' => 'topics',
'parseBBCode' => true,
'parseHtml' => false,
'parseEmoticons' => true ) );
and finally where the text you want to parse from BBcode -> HTML use:
print $parser->BBCodeToHtml( $toParse );
Related
I know this question is frequently asked on this forum, but none of the proposed solutions has worked for me, basically, I am a WordPress plugin developer and developing a plugin whose session starts in the init file but close when the client closes his/her browser, and it is necessary to keep the session open. In this case, how can I get rid of this warning? Maybe I should use something other than the session or what you suggest.
Till now there are different proposed solutions like Use
if ( !session_id() ) {
session_start( [
'read_and_close' => true,
] );
}
but with this technique, I can never be able to use sessions in my plugin, and the whole functionality goes on the ground.
Any help in this regard would be much appreciated.
thanks
WordPress plugin or theme developers cannot use php's session subsystem as you have discovered.
If you need to save some kind of global context for your plugin to work, use WordPress's add_option() function to save it at the end of each operation. You can retrieve your context using get_option().
If you need per-user context, use add_user_meta() to save it and get_user_meta() to retrieve it. Like this:
$id = get_current_user_id();
$myContext = get_user_meta( $id, 'my_plugins_meta_tag_name' );
...
add_user_meta( $id, 'my_plugins_meta_tag_name', $myContext);
WordPress uses a database table to store these sorts of options. Some sites put an object cache in front of the table. But these APIs are how you do what you want to do.
In your plugin's deactivation hook, please consider calling delete_option() to clean up that storage. And, if you use per-user context, please consider using delete_user_meta() to remove your user_meta item from all of them.
$users = get_users( array( 'fields' => array( 'ID' ) ) );
foreach($users as $user){
delete_user_meta ( $user->ID, 'my_plugins_meta_tag_name' );
}
As for sensitive information, WordPress offers no other place to put it. And, if you were using sessions, php would need to put it somewhere as well. By default, php puts session data in the filesystem of the server. That's why WordPress hashes user passwords before storing them.
i've created a woocommerce plugin,
the plugin itself runs perfectly.
However as long as it takes for it to run, i'm experiencing wierd performances issues.
The website is totally inaccessible(both frontend and backend) ONLY from the browser that is logged in and ran the plugin.
Both frontend and backend are loading until the plugin finishes.
to make things even wierder it's working perfectly from another browser.
also this is run on a high-end dedicated server and when it's running the loads on the server are very low.
any clues?
require __DIR__ . '/vendor/autoload.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-config.php');
use Automattic\WooCommerce\Client;
$woocommerce = new Client(woocommerce_api_url, api_key, api_secret,['version' => 'wc/v3','timeout' => '99999',]);
echo '<pre>';
$db = new DBfdr();
$i=0;
$page = 1;
$products = [];
$all_products = [];
do{
try {
$products = $woocommerce->get('products',array('per_page' => 100, 'page' => $page));
} catch(HttpClientException $e) {
die("Can't get products: $e");
}
$all_products = array_merge($all_products,$products);
$page++;
Notes: the DBfdr class contains a simple function for managing pdo connections to the sql server.
That piece of code doesn't look so good, why would you need to make a plugin that externally loads wp then going through Woocommerce Client Api to get the list of products and then cycle all of them to make an if condition.
That would be so much more efficient if you would just pass inside WP standard plugin structure, using global $wpdb class and performing your queries with some join.
That said, the problem you are facing could be solved by adding this before your code:
ignore_user_abort( true );
/* Don't make the request block till we finish, if possible. */
if ( function_exists( 'fastcgi_finish_request' ) && version_compare( phpversion(), '7.0.16', '>=' ) ) {
fastcgi_finish_request();
}
The problems occurs because the wp-config.php is included in the beggining.
wp-config.php in the end has
require_once(ABSPATH . 'wp-settings.php');
which inits some core wordpress functions which in turn "locks" the session.
i managed to fix the issue by removing
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-config.php');
and replacing it with customs defines
I am working on a project where users can check and edit their own data and can also view their invoices.
I am writing the code for this project myself in PHP on my local machine but the invoices are made with the wp-invoice plugin on an existing and working wordpress site that's hosted somewhere else.
I want users to be able to log in to my web-app and then see their invoices that the web-app retreived from the wp-invoice api on the public wordpress site. The documentation for the wp-invoice api can be found here: https://www.usabilitydynamics.com/product/wp-invoice/docs/wp-invoice-api, it uses XML-RPC with which I am not familiar.
XML-RPC uses built-in wordpress functions to use the API. So I also downloaded and installed wordpress on my local machine and almost got it working (i believe). But when I load the wordpress functions into my web-app using "wp-load.php" it redirects me to the wordpress installer which says wordpress is already installed and there is a working wp-config.php file. This is correct because I installed wordpress successfully.
My code:
(This is my only code, there are no other files, functions or classes included using 'include_once')
define("ABSPATH", "C:/wamp64/www/mijnDashboard/WP");
define("WPINC", "/wp-includes");
include_once( ABSPATH . '/wp-load.php' );
include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
$client = new WP_HTTP_IXR_Client( 'http://MyWordpressSiteWithWP-INVOICEinstalled/xmlrpc.php' );
$client->query('wp.invoice', array(
$method = 'get_invoice',
$credentials = array('Username', 'Password'),
$args = array(
'ID' => 1032017043
)
));
$the_invoice = $client->getResponse();
echo "$the_invoice";
echo "hallo";
Result: (it redirected me from my self-written 'dashboard.php' to the wordpress page 'wp-admin/setup-config.php')
Tried solutions:
I searched the web for solutions and some people said it had something to do with my browser cache. I tried another browser and incognito mode in chrome but both got me the same result.
Beside that I tried to load the wordpress functions that I need to use XML-RPC using an other wordpress file called 'wp-blog-header.php' but it got me the same result.
I actually think this is easy to fix but I am new to XML-RPC and API's at all and I don't know how to properly include all wordpress functions in a non-wordpress, self-written file.
Thanks,
Elias
Okay, I figured it out. I didn't use my local machine anymore but I used a webserver instead. There are no errors and I am not being redirected when I load 'wp-load.php'.
It is strange that almost the same code runs fine on a public webserver but not on a local machine. But it's working now so it's fine.
The code: (I used this and uploaded it to the WordPress root directory which contains the other files like wp-config.php etc.)
<?php
define( 'WP_USE_THEMES', false );
require( 'wp-load.php' );
$rootD = $_SERVER['DOCUMENT_ROOT'];
define("ABSPATH", "$rootD/httpdocs");
define("WPINC", "/wp-includes");
include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
$client = new WP_HTTP_IXR_Client( 'http://mywordpresswebsite/xmlrpc.php' );
$client->query('wp.invoice', array(
$method = 'get_invoice',
$credentials = array('username', 'password'),
$args = array(
'ID' => 1032017039
)
));
$the_invoice = $client->getResponse();
foreach ($the_invoice as $key => $value) {
echo "$value";
}
?>
Thanks for all the help.
I want to fetch the data in my WordPress site and display it in android using JSON. My problem is I don't get how I get the particular posts data with all Images in that post using the custom QUERY and convert it into JSON using PHP.
My site have some custom post types example portfolio, I also want to fetch all data for portfolio item.
If you have any ideas or solutions for my problem please tell me.
There is a REST API in the works for WordPress that outputs JSON.
Currently it is a plugin, but its future is to be integrated into core.
Development
https://github.com/WP-API/WP-API
Beta Releases
https://wordpress.org/plugins/json-rest-api/
Documentation
http://wp-api.org/guides/getting-started.html
Open WP Rest Api plugin with plugin editor and add this code in your plugin file;
add_action( 'init', 'register_my_portfolios' );
function register_my_portfolios() {
$args = array(
'public' => false,
'show_in_rest' => true,
'label' => 'Portfolios'
);
register_post_type( 'portfolio', $args );
}
now you can query your portfolio post types
I manage the website for PAWS New England, an animal rescue organization who relies heavily on PetFinder. While PetFinder offers an iframe based widget for display your available animals, it breaks the responsive design of the site on smaller screens.
Because of this, I've built custom "Our Dogs" page by using PetFinder's API (XML based).
Unfortunately, the API can run pretty slow at times. I would like to pull data from the API once an hour and store it on the site's mySQL database (it's powered by WordPress), and run the custom page off that instead of the live API data. WordPress's "Transient API" seems like a perfect fit, but I can't for the life of me figure out how to make it work.
After some searching, it seems like the problem may be in PHP's ability (or lack there of) to store XML data. In other words, I may need to convert the data to a string or array first.
I'm now officially stuck. Anyone have any insights on how to save XML data to the WordPress database once and hour, and access that data for use in a function?
Here's my existing code. Thanks in advance!
<?php
function petf_shelter_list( $atts ) {
extract( shortcode_atts( array(
'shelter_id' => '1234',
'api_key' => 'abcdef',
'count' => 150,
'status' => 'A'
), $atts ) );
$xml = simplexml_load_file( "http://api.petfinder.com/shelter.getPets?key=" . $api_key . "&count=" . intval($count) . "&id=" . $shelter_id . "&status=" . $status . "&output=full" );
// Stuff I do with $xml here...
}
add_shortcode('shelter_list','petf_shelter_list');
?>
You can use PHP's XML Parser to deal with the XML portion of your problem.
As for running something every hour, you need to setup a cron job to execute your desired script.