searchable file content wordpress - wordpress

I am building a wordpress site and I have many files I need to upload and these files need the content to be searchable for users. Right now they are in pdf format. (format does not matter as long as these files can not be altered)
example: If the user types in a keyword or phrase the file content will be searched and return the file to the user that the keyword matched.
I searched for solutions and came up empty, maybe someone can point me in the right direction.

Its preety simple, you just have to install pdftotext (here is a good tutorial) and then you can search for any text. So if you a have a list of all your documents in an array you can loop and search for a certain string.
I have not tested this code, but i guess it should work fine.
<?php
$files = array('relative-link-to-file1', 'relative-link-to-file2', 'file3');
$text_to_search = 'test';
$found = array();
foreach($files as $file){
$content = shell_exec('/usr/local/bin/pdftotext '.$file.' -');
$found[]['position'] = strpos($content, $text_to_search );
$found[]['file'] = $file;
}
// To echo all the found instances.
foreach($found as $file){
echo 'The text has been found at position '.$file['position'].' within the file '.$file['file'];
}
?>

Related

Getting data from projects meta data Wordpress

i have been working on this for a while now and cant seem to wrap my head around it.
I am using wordpress build in projects to create "people" for a website.
I then have made a function that runs on project publish, where i need it to create a .vcf file to use as VCards for these peoples pages.
I have managed to make it create the vcf file and input some hardcoded data but i need it to get info from custom fields meta data i have made using Advanced Custom fields
I need the file to output something like this:
BEGIN:VCARD
VERSION:2.1
N:Nørbygaard;Tobias
FN:Tobias Nørbygaard
ADR;WORK;PREF:;;;Sunds;;7451;Danmark
ORG:Offbeat Media
TITLE:Webudvikler
TEL;WORK;VOICE:+45 27 50 74 57
TEL;CELL;VOICE:
URL;TYPE=WORK:www.offbeatmedia.dk
EMAIL;PREF;INTERNET:tobias#offbeatmedia.dk
END:VCARD
This is the code i have so far
function dothisfunction() {
// Do stuff
$ext = ".vcf";
$filename = "tobias".$ext;
$path = wp_upload_dir();
$file = fopen($path['path'].$filename,"w+");
echo fwrite($file, "This is where the data needs to be input");
fclose($file);
chmod($file,0777);
}
add_action( 'publish_project', 'dothisfunction' );

Id changing variable

I'm working on a project where I upload a file and use its path in a shortcode. Right now I've hard-coded the post's ID into my code but I want to make it dynamic so that new posts automatically get the correct shortcode.
<?php
global $wpdb;
$thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = 5574" ) );
echo do_shortcode ('[sgpx gpx="'.'/wp-content/uploads/' . $thepost->meta_value . '"]');
?>
Going off of what #Damocles said, you are really setting yourself up for trouble with your current path. Instead, there are built-in WordPress functions that abstract away the database and utilize caching that you are strongly encouraged to use.
// Get the current WordPress post
$the_post = get_post();
//If we have a value (there are cases where this will be empty)
if($the_post){
// Get the value from the post meta table by the current post's ID
$the_post_meta = get_post_meta($the_post->ID, 'YOUR_META_KEY_HERE', true);
// Double-check that we have a value
if($the_post_meta) {
// Finally, echo the shortcode
echo do_shortcode ('[sgpx gpx="'.'/wp-content/uploads/' . $the_post_meta . '"]');
}
}
There are several ways to get the current post however get_post() is the most common. If you are in a custom loop, however, you might need to adjust accordingly.
To access the meta, use get_post_meta() which includes some optimizations including use a cache instead of the database.
Although there are definitely exceptions, generally speaking, if you are working with WordPress and you find yourself writing SQL statements, there is almost always a better, safer, faster, etc. way to do it using core functions.

trying to display a custom field

Guys I'm writting a wordpress site to run a knowldge base for our Service desk. As one person will be updating it i needed to have a field for who wrote the kb artical. I'm tring to add a custom field into my wordpress theme to display writtenby using Advance custom Fields. Now I'm using echo Knowledge Base plugin for knowldge base.
I've got as far add ing code below will display the text below the last up date value that plugin creates. However i cannot get it to put value from the custom field on the page after this. The plugin creates the page using php below the ive added the two lines as below.
$wb = get_post_meta($post->ID, 'Writtenby', true);
echo ' Last Update Writtenby:'.$wb.' ';
// LAST UPDATED ON
public static function last_updated_on( $args ) {
echo '' . esc_html( $args['config']['last_udpated_on_text'] ) . ' ' . EPKB_Utilities::get_formatted_datetime_string( $args['article']->post_modified, 'F d, Y' ).'';
$wb = get_post_meta($post->ID, 'Writtenby', true);
echo ' Last Update Written by:'.$wb.' ';
}
Advanced Custom Fields plugin use a little bit different system to store postmeta. Try to use get_field() instead get_post_meta()
If you have the ID
$customField = get_post_meta($my_id, "_mcf_customField", true);
However if you want to get the ID from the object:
$customField = get_post_meta($post_id->ID, "_mcf_customField", true);
After much more work looks like at the point the page was being created it had no reference to partical page not even a current one. They where writting to an array all artical numbers and info.
So by telling get_field which artical to get the writtenby field from it now displayed data and not blank
$wb= get_field('Writtenby', $args['article']->ID);

How to get data from array object in WordPress plugin api

http://localhost/wordpress/give-api/forms/?key=15443f18029e6f5d3b65d04e1640ffbe&token=c3de770a410282359413c74a588c5c74
The above link is a plugin api link. Above link won't work to your browser.
when I set the above link in the browser , it returns array object like http://postimg.org/image/6ozmjy0e7/ .
My question is , how can I set this url in a variable in wordpress and how can I get the data from that array object. I just want to get the data from that array object. If any other process is available, then please suggest me. Thanks...
In functions.php:
function displayApiUrl() {
global $apiUrl; // you probably don't actually need to set it global as it is a function
$apiUrl = 'http://localhost/wordpress/give-api/forms/?key=15443f18029e6f5d3b65d04e1640ffbe&token=c3de770a410282359413c74a588c5c74';
return $apiUrl;
}
In your theme you can now use:
<?php $api = displayApiUrl(); ?>
With that you can process your array in a foreach loop:
<?php
$json_url = file_get_contents($api);
$json_data = json_decode($json_url, true);
foreach ($json_data['forms'] as $form) {
$form_id = $form['info']['id'];
echo $form_id;
}
?>
The new "standard" for WordPress rest apis is Json Rest API, which will be partially integrated into WordPress core in the next release.
You can get it here https://wordpress.org/plugins/json-rest-api/ and documentation at http://wp-api.org/
In terms of the question how to put array information into the URL, the format is
http://www.example.com/wp-json/endpoint?array_1[key1]=Pensacola&array_1[key2]=Florida
The URL of course changes, and the wp-json/endpoint is replaced with whatever the final endpoint is for which ever rest api you choose to use.

simplexml_load_file not working to read rss feed at www.thetechjournal.com

When I try to read the rss feed at www.thetechjournal.com I get nothing. They use wordpress to generate the rss, so I assume it has something to do with that.
<?php
$url = 'http://feeds.thetechjournal.com/TheTechJournal';
$rss = simplexml_load_file($url);
print_r($rss);
?>
UPDATE: The offending XML is attached as an image.
Their feed has errors. Check it with this:
$url = 'http://feeds.thetechjournal.com/TheTechJournal';
libxml_use_internal_errors(true);
$sxe = simplexml_load_string($url);
if ($sxe === false) {
echo "Failed loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
}
print_r($rss);
I am from TTJ team. When I do run this Validator it shows fine, but as we have some iFrame is marks those issue. And sometime if some post have any weird char that what makes it invalid. Please point out to me any exact issue that we could look into. I do appreciate your concern.
http://validator.w3.org/feed/check.cgi?url=http%3A%2F%2Ffeeds.thetechjournal.com%2FTheTechJournal

Resources