Hope you all have been doing great...
I am here today to look for an answer to my issue...
I created a plugin and activated it, it does not create a table etc just simple php script.
<?php
/*
Plugin Name: F
Plugin URI: h
Description: T
Author: D
Author URI: h
*/
$server = "localhost";
$user = "admin";
$password = "";
$db = "wordpress";
$con = mysql_connect($server,$user,$password);
if (!$con) {
die("database connection error");
} else
{
mysql_select_db($db, $con);
$results = mysql_query("SELECT ID, post_title FROM wp_posts "
. "WHERE "
. "post_status = 'publish' "
);
while($row = mysql_fetch_array($results))
{
echo $row['post_title'];
}
}
the autocomplete code is as below
$("#imageSearch").autocomplete("<?php echo bloginfo('wpurl')."/wp-content/plugins/foxycomplete/"; ?>foxycomplete.php", {
dataType: "json",
parse: function(data) {
return $.map(data, function(row) {
return {
data: row,
value: row.title,
result: $("#imageSearch").val()
}
});
}
}).result(function(e, item) {
location.href = link(item);
});
});
this is working but I am pretty sure that this is not the right way. I am not able to use the wp functions is the plugin script and also this seems unsafe and prone to hacking...
could anyone please help how I can get a php file to feed the autocomplete that can access wop functions and is safe?
Thanks a lot!
There's no such thing as a Plugin Page. The code above should probably be wrapped in a function and called from somewhere in a WordPress context, or it should be used in an action or a filter.
If you are accessing the plugin page directly rather than from within Wordpress, the problem might be that the $wpdb is not being initialized. This is done in the WordPress header, which is normally included on the page if you are inside of a Wordpress template. Try including wp-blog-header.php in your script with something like this:
include_once(‘wp-blog-header.php’);
Related
I am certain I am making a basic mistake here. I am getting the lovely 400 Bad Request response from WordPress when I try to make an Ajax call. I have one Ajax call that works, and this one that does not. I have looked at several posts both here and elsewhere, but remain stumped as to where I have erred.
Okay, the relevant code. First, here is how I do the add_action and script registration:
if ($_GET["page"] == "krudkat_data_structures") {
add_action("wp_ajax_krud_save_new_connection", "krud_save_new_connection");
}
// Other Stuff, for the Ajax call that works.
if ($_GET["page"] == "krudkat_data_structures") {
wp_register_script("krud_data_structures", plugin_dir_url( __DIR__ ) . "/js/dataStructures.js", array("jquery"));
wp_localize_script("krud_data_structures", "krudAjax", array( "ajaxurl" => admin_url("admin-ajax.php")));
wp_enqueue_script("krud_data_structures");
}
This sits in a function outside of a class. My JS call is like this:
var krudNewConnect = { action:"krud_save_new_connection",
dbname:$("#krud_new_dbname").val().trim(),
dbhost:$("#krud_new_dbhost").val().trim(),
dbconnect:$("#krud_new_dbconnect").val().trim(),
dbusername:$("#krud_new_dbusername").val().trim(),
dbpassword:$("#krud_new_dbpassword").val() };
$.post(krudAjax.ajaxurl, krudNewConnect, function(newConnectData) {
console.log(newConnectData);
});
Finally, my PHP method is, thus far, very simple:
function krud_save_new_connection() {
$dbname = $_POST["dbname"];
$dbhost = $_POST["dbhost"];
$dbconnect = $_POST["dbconnect"];
$dbusername = $_POST["dbusername"];
$dbpassword = $_POST["dbpassword"];
$dbport = 0;
$dbsocket = '';
if (!empty($dbconnect)) {
if (is_numeric($dbconnect)) {
$dbport = $dbconnect * 1;
} else {
$dbsocket = $dbconnect;
}
}
echo "Port: " . $dbport . "\nSocket: " . $dbsocket;
wp_die();
}
This is not expected to work when somebody is not logged in as an admin, so I omitted the no_priv add_action. I am certain this is not the problem; I did add that into my code and it had no impact.
What newbie mistake have I made? :)
First, enqueue the script and then localize the script for ajax. It will fix the issue. Check the codex: https://codex.wordpress.org/AJAX_in_Plugins
I am adding a filter in wordpress plugin.
Filter:
add_filter('wp_insert_post_data', 'saveuser_filter');
And calling it in functions.php file with below code:
function saveuser_filter($use_number, $use_code){
$use_num = $use_number;
$use = $use_code;
global $wpdb;
$mydb = new wpdb('username','password','database','host');
$stmt1 = $mydb->query("INSERT INTO wpconfig (use_num, use) VALUES ('$use_num', '$use')");
$resu = $mydb->query($mydb->prepare($stmt1));
if($resu)
{
$resu="Data Inserted Successfully:";
echo json_encode($resu);
}
else {
$error="Not Inserted,Some Probelm occur.";
echo json_encode($error);
}
}
However, I am saving these details after user completes an order on woocommerce. But anyhow, its giving error: Unable to create order. and order is not getting placed. Please suggest what I am doing wrong here.
I use code below to post via XML-RPC, it does success. But when I send a long string $bodypost, my post doesn't have body content. I test it with html code, it's working, then I remove all space, my $bodypost just have 1 line with about 4000 words, it's not working.
How can I fix it?
<?php
function send_post($titlepost, $bodypost, $categorypost, $keywordspost )
{
require_once("IXR_Library.php.inc");
$encoding='UTF-8';
$client->debug = false; //Set it to false in Production Environment
$title= $titlepost; // $title variable will insert your blog title
$body= $bodypost; // $body will insert your blog content (article content)
$category=$categorypost; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
$keywords=$keywordspost;
$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format
$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
$content = array(
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => array($customfields)
);
// Create the client object
$client = new IXR_Client('http://www.domain.com/xmlrpc.php');
$username = "abc";
$password = "abc";
$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immideately, to save as draft set it as 'false'
// Run a query for PHP
if (!$client->query('metaWeblog.newPost', $params)) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
}
else
echo "Article Posted Successfully";
}
?>
I found how to fix it. This code does not work if my body code is give by source code of default Wordpress editor, but if I change it to CkEditor it's working!
I have a plugin installed on my wordpress blog that takes the email address and message and creates a new user on my other CMS platform. Problem is this effects all comment boxes. I want to re-work this plugin to check if its on a particular page if so some values would be different. If not then proceed like usual.
function ifcj_createUser_SFI($comment_id) {
# determine commenters email
global $wpdb;
$sql = "SELECT comment_author_email FROM $wpdb->comments WHERE comment_ID = $comment_id";
$userEmail = $wpdb->get_var($sql);
if ( $userEmail == NULL ) {
return false;
}
# pull in class file
require( ABSPATH . 'class.convio_api.php');
# create instance and connection with convio, ALWAYS REQUIRED
$c = new ConvioAPI('site_id=xxx&api_key=xxxxxx&login_name=xxx#xxx.org&login_password=xxxxx');
# add user to groups/interests (this can be done when creating the user also)
$c->createUpdateUser('primary_email='.$userEmail.'&add_group_ids=73663&add_interest_ids=3761');
return true;
}
add_action('comment_post','ifcj_createUser_SFI',0,1);
Thanks in advance.
Use conditional tags: http://codex.wordpress.org/Conditional_Tags
Here is how you would test for a certain page: http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page
Like:
if ( is_page( '2' ) ) {
return false;
}
I know how to change the base of the author permalinks, however on my site, I refer to users not by their username but by a number based on their User ID, so User number 5 wrote this post, rather than JohnDoe123 wrote this post.
The problem comes when I go to that users archives and instead of seeing something like example.com/authors/5/ I see example.com/authors/johndoe123/ .
How do I change permalinks so that I can pull up author archives using the following structure? :
[wordpress_site_url]/authors/[user_ID]/
This can be done by adding new rewrite rules for each user in exactly the same way you would when changing or removing the author base. So, adapting code from a previous answer, you would add your rewrite rules something like this:
add_filter('author_rewrite_rules', 'my_author_url_with_id_rewrite_rules');
function my_author_url_with_id_rewrite_rules($author_rewrite) {
global $wpdb;
$author_rewrite = array();
$authors = $wpdb->get_results("SELECT ID, user_nicename AS nicename from {$wpdb->users}");
foreach ($authors as $author) {
$author_rewrite["authors/{$author->ID}/page/?([0-9]+)/?$"] = 'index.php?author_name=' . $author->nicename . '&paged=$matches[1]';
$author_rewrite["authors/{$author->ID}/?$"] = "index.php?author_name={$author->nicename}";
}
return $author_rewrite;
}
And then filter the author link:
add_filter('author_link', 'my_author_url_with_id', 1000, 2);
function my_author_url_with_id($link, $author_id) {
$link_base = trailingslashit(get_option('home'));
$link = "authors/$author_id";
return $link_base . $link;
}
Actually I don't think you need to create rules for each user in this case, the following two rules should suffice:
add_filter('author_rewrite_rules', 'my_author_url_with_id_rewrite_rules');
function my_author_url_with_id_rewrite_rules($author_rewrite) {
$author_rewrite = array();
$author_rewrite["authors/([0-9]+)/page/?([0-9]+)/?$"] = 'index.php?author=$matches[1]&paged=$matches[2]';
$author_rewrite["authors/([0-9]+)/?$"] = 'index.php?author=$matches[1]';
return $author_rewrite;
}