Getting data from projects meta data Wordpress - 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' );

Related

Import data from csv into a specific post

I have multiple posts that has multiple custom fields on it. Now i need to add these fields from a excel/csv file. These files contain lots of info about this post. The problem i run into is that i don't see a way to import this content automatically. I don't seem to be able to use wp all import for this because it create a new item for each record and thats not what i want. All records need to be added as a custom field/repeater field.
If anyone has an idea on how to do this it would be much appreciated.
I tried to import this data using WP all import without succes.
function add_some_fields($file_url) {
$data= array_map('str_getcsv', file($file_url));
foreach ($data as $item) {
$post_id = $item[0]; // I hope you have post_id in that CSV file somewhere
$field_name = $item[1] ; // ACF field name IF YOU HAVE IT IN CSV if not set manually, or make an array with names
$col1 = $item[2]; //Value
add_row( $field_name, $col1, $post_id ); //add row (https://www.advancedcustomfields.com/resources/add_row/)
}
}
Call from where you want and how you want, usually I use condition in footer checking if custom GET parameter passed and if Yes then run function.

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);

searchable file content 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'];
}
?>

Get localized string for specific locale

How can I use the localization mechanism in WordPress to get access to an existing but not-current language string?
Background: I have a custom theme where I use locale 'en_US' as the default locale and translate through a PO file to locale 'es_ES' (Spanish).
Let us say I use the construction
__('Introduction', 'my_domain');
in my code, and that I have translated 'Introduction' to the Spanish 'Introducción´ in my PO file. All this works fine.
Now to the problem: I want to insert n records in my database with all existing translations of the string 'Introduction' - one for each language; so, n = 2 in my example.
Ideally, I would write something like this:
$site_id = 123;
// Get an array of all defined locales: ['en_US', 'es_ES']
$locales = util::get_all_locales();
// Add one new record in table details for each locale with the translated target string
foreach ($locales as $locale) {
db::insert_details($site_id, 'intro',
__('Introduction', 'my_domain', $locale), $locale);
}
Only, that the 3rd parameter in __() above is pure fantasy on my part. You can only validly write
__('Introduction', 'my_domain');
to get either 'Introduction' or 'Introducción' depending on the current locale.
The outcome of the code above would ideally be that I end up with two records in my table:
SITE_ID CAT TEXT LOCALE
123 intro Introduction en_US
123 intro Introducción es_ES
I am aware that I want something that requires loading all the MO files, where normally, only the MO file for the current language is required. Maybe use of the WordPress function load_textdomain is necessary - I was just hoping there already exists a solution.
Expanding on the question by including the plugin PolyLang: is it possible to use Custom Strings to achieve the above functionality? E.g. conceptually:
pll_('Introduction', $locale)
Old question I know, but here goes -
Starting with a simple example where you know exactly what locales to load and exactly where the MO files are, you could use the MO loader directly:
<?php
$locales = array( 'en_US', 'es_ES' );
foreach( $locales as $tmp_locale ){
$mo = new MO;
$mofile = get_template_directory().'/languages/'.$tmp_locale.'.mo';
$mo->import_from_file( $mofile );
// get what you need directly
$translation = $mo->translate('Introduction');
}
This assumes your MO files are all under the theme. If you wanted to put more of this logic through the WordPress's environment you could, but it's a bit nasty. Example:
<?php
global $locale;
// pull list of installed language codes
$locales = get_available_languages();
$locales[] = 'en_US';
// we need to know the Text Domain and path of what we're translating
$domain = 'my_domain';
$mopath = get_template_directory() . '/languages';
// iterate over locales, finally restoring the original en_US
foreach( $locales as $switch_locale ){
// hack the global locale variable (better to use a filter though)
$locale = $switch_locale;
// critical to unload domain before loading another
unload_textdomain( $domain );
// call the domain loader - here using the specific theme utility
load_theme_textdomain( $domain, $mopath );
// Use translation functions as normal
$translation = __('Introduction', $domain );
}
This method is nastier because it hacks globals and requires restoring your original locale afterwards, but it has the advantage of using WordPress's internal logic for loading your theme's translations. That would be useful if they were in different locations, or if their locations were subject to filters.
I also used get_available_languages in this example, but note that you'll need the core language packs to be installed for this. It won't pick up Spanish in your theme unless you've also installed the core Spanish files.

Can Drupal change the template used based on a file extension in the url?

I am using Drupal 6.16 with a number of modules installed. I was trying to find out if there is a way to change the output of a node when a different file extension is added to the url. For example:
http://example.com/drupal?q=foo/bar - returns a normal drupal node
http://example.com/drupal?q=foo/bar.xml - returns xml output of the node
Is this even possible with Drupal? Do I have to hack the core code to get this working?
You should not need to hack the core code. There are probably several contributed modules that can do this for you.
To output an XML version of a node, check out the Views Bonus Pack module, which extends the Views module. It has basic export capabilities, including CSV, TXT, DOC, and XML. The documentation is brief, but there is a README.txt file in the views_bonus/export/ directory that gives the basic steps for creating a feed in a view that will output XML.
You can set the path for the feed, so while I don't believe the .xml extension will work, you could set up a path with an additional component like this:
http://example.com/drupal?q=foo/bar <-- normal output
http://example.com/drupal?q=foo/bar/xml <-- XML output
To change the template file that is used for a node based on the path, you can use a preprocess function in your template.php file to add a template suggestion based on the path. This takes a bit more understanding of how the template files work, but ultimately you'll have more control of the output than you will with a view.
Here is how I fixed this.
Add the custom_url_rewrite_inbound function to check for incoming request ending with .xml. If it finds a request ending with .xml it strips that off, so that the correct data can be located by the rest of the drupal machinery. It also sets 'subsite_xml_request' to true so that the appropriate theme template can be used later.
function custom_url_rewrite_inbound (&$result, $path, $path_language) {
if(preg_match('/\.xml$/', $path)) {
$search = preg_replace('/^(.*)\.xml$/', "$1", $path);
if ($src = drupal_lookup_path('source', $search, $path_language)) {
$_REQUEST['xml_request'] = true;
$result = $src;
}
}
Modify the phptemplate_preprocess_page function in your template.php to add additional '-xml' templates.
function phptemplate_preprocess_page(&$vars) {
if ($_REQUEST['xml_request']) {
if (module_exists('path')) {
$path = str_replace('/edit','',$_GET['q']);
$alias = drupal_get_path_alias($path);
if ($alias != $_GET['q']) {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$vars['template_files'][] = $template_filename . '-xml';
}
$vars['template_files'][] = 'page-xml';
}
}
}
}
Create the required page-xml.tpl.php

Resources