I'm working on WordPress actually and created a plugin from scratch, fully working on admin page.
The plugin allow you to add country and languages related (USA->English|Sapnish for example)
I am using Class and constructor for the plugin's functions
Now I would like to created a select with country and depending on country another select with languages. I know how to create this in js/html but I can't find a way to get the data from the plugin.
I tried :
Create a function (in the plugin) who used plugin's other functions (see below)
public function getCountry()
{
global $wpdb;
$table_name = $wpdb->prefix . "country_wmu";
$results = $wpdb->get_results('SELECT * FROM ' . $table_name);
if ($wpdb->last_error !== '') {
echo '<p class="info-wmu error">Une erreur est survenue</p>';
} else {
return $results;
}
}
public function getLang($id)
{
global $wpdb;
$table_name = $wpdb->prefix . "lang_wmu";
$results = $wpdb->get_results('SELECT * FROM ' . $table_name . ' WHERE _id_country = ' . $id);
if ($wpdb->last_error !== '') {
echo '<p class="info-wmu error">Une erreur est survenue</p>';
} else {
return $results;
}
}
public function wmu_get_data($atts, $content=null)
{
$country = $this->getCountry();
foreach ($country as $key => $value) {
$country_id = $value->id;
$lang = $this->getLang($country_id);
foreach ($lang as $key2 => $value2) {
$country[$key]->lang = array(
'name' => $value2->name,
'url' => $value2->url
);
}
}
return $country;
}
and then add shortcode
add_shortcode('wmu_data', 'wmu_get_data');
and displaying the shortcode in the page, but shortcode displays as plain text
[wmu_data]
So I tried to do a function in my functions.php to do a do_shortcode, then add a shortcode to this function.
function test()
{
$country = do_shortcode('[wmu_data]');
echo '<pre>';
print_r($country);
echo '</pre>';
}
add_shortcode('test','test');
But nothing happens, blank page...
I really don't have many more ideas to do it...
EDIT:
I found out with the error code, Have to use
add_shortcode('wmu_data', array('WB_MultiLang_URL','wmu_data'));
to call it outside the function
thanks Larjos for pointing out the problem at his root :)
Related
I have a custom checkbox field created in Wordpress where the user can select various facilities available at a listed property.
I am building a page template using Elementor.
When I import the data from ACF, it displays as a comma separated list.
Is there any way I can instead get this to display as as a bulleted list?
Here is a code to get the output data from ACF as a bulleted list -
<?php
$arr = get_the_field('field_name');
$str = explode (",", $arr);
echo '<ul>';
foreach($str as $s){
echo '<li>'.$s.'</li>';
}
echo '</ul>';
?>
I hope this is what you are looking for.
You could try using a plugin that let's you create php code snippets and run them with a shortcode such as this: https://it.wordpress.org/plugins/insert-php/
Once you created the php snippet you could try to run it with a shortcode using Elementor's shortcode widget.
I'd slightly adjust Tahmid's excellent answer. In order to allow for empty lines (without a bullet) use this in you functions.php file:
/**
* create a ACF text field with bullets
* Empty lines stay empty
* #param string $field_name Name of the field to bullet
*/
function acf_bullets(string $field_name): void {
$format_acf_bullet = function(
string $value,
int $post_id,
array $form ):string {
if( empty($value) ) {
return '';
}
$lines = explode( "\n", $value );
$result = "<ul class=\"theme-ul\">\n";
foreach( $lines as $line) {
if( strlen($line)<=1 ) { // empty line, start a new list
$result .= "</ul><p/><ul class=\"theme-ul\">\n";
} else {
$result .= "<li>".$line."\n";
}
}
$result .= "</ul>\n";
return $result;
};
add_filter("acf/format_value/name=$field_name", $format_acf_bullet, 10, 3);
}
Call this with acf_bullets('your-fieldname-here');
What i want to do is create pdf (pdf's) on woocommerce_email_attachments hook, then create zip file with these pdf's, and then on thankyou page locate button to download this zip. To generate zip I used woocommerce_before_thankyou hook but this is too late. So I tried
woocommerce_order_status_on-hold,
woocommerce_order_status_processing
But these hooks seems not firing, maybe they work on transitions in admin order page. Also I tried woocommerce_payment_complete with test paypal account - not created zip also.
Or maybe these hooks fire before woocommerce_email_attachments and there is no pdf yet to create zip? So can you suggest hook after sending wc email but before woocommerce_before_thankyou ?
Code which I use to generate pdf's:
add_filter('woocommerce_email_attachments', 'attach_order_notice', 10, 3);
function attach_order_notice($attachments, $email_id, $order)
{
// check if all variables properly set
if (!is_object($order) || !isset($email_id)) {
return $attachments;
}
// Skip User emails
if (get_class($order) == 'WP_User') {
return $attachments;
}
// do not process low stock notifications, user emails etc!
if (in_array($email_id, array('no_stock', 'low_stock', 'backorder', 'customer_new_account', 'customer_reset_password'))) {
return $attachments;
}
// final check on order object
if (!($order instanceof \WC_Order || is_subclass_of($order, '\WC_Abstract_Order'))) {
return $attachments;
}
// if ($email_id == 'customer_on_hold_order') {
$order_items = $order->get_items(['line_item']);
$date = $order->get_date_created()->date('h-i-s,j-m-y');
foreach ($order_items as $product) {
$html = render_template(locate_template('pdf/certificate/certificate.php'), ['order' => $order, 'order_product' => $product]);
$html = utf8_decode(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$options = new Dompdf\Options([
'tempDir' => get_template_directory() . '/certificate',
'fontDir' => get_template_directory() . '/assets/fonts/',
'chroot' => get_template_directory(),
'defaultFont' => 'Montserrat',
'isRemoteEnabled' => true,
'isHtml5ParserEnabled' => true
]);
$dompdf = new Dompdf\Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$data = $dompdf->output();
$pdf_path = get_template_directory() . "/certificate/product-{$product->get_product_id()}({$date}).pdf";
file_put_contents($pdf_path, $data);
$attachments[] = $pdf_path;
}
// }
return $attachments;
}
Code which I use to create zip:
add_action('woocommerce_before_thankyou', 'custom_make_thank_page');
function custom_make_thank_page($order_id)
{
$order = wc_get_order($order_id);
$order_items = $order->get_items(['line_item']);
$date = $order->get_date_created()->date('h-i-s,j-m-y');
$total_quantity = array_sum(array_map(function ($product) {
return $product->get_quantity();
}, $order_items));
//Zip
$zip = new ZipArchive();
$file = TMP_PATH . '/pdf/zip/certificates-' . $order->get_id() . '.zip';
if ($zip->open($file, ZipArchive::CREATE) === TRUE) {
foreach ($order_items as $item) {
$file_pdf = get_template_directory() . "/certificate/product-{$item->get_product_id()}({$date}).pdf";
if (file_exists($file_pdf)) {
$zip->addFile($file_pdf, "/certificate/product-{$item->get_product_id()}({$date}).pdf");
}
}
$zip->close();
}
?>
<?php if (!$order->has_status('failed')) : ?>
<a href="<?php echo TMP_URL . '/pdf/zip/certificates-' . $order->get_id() . '.zip'; ?>" class="button button-thank-page"><span>Download certificate</span>
</a> // Here downloading not works, works only after reloading page
</div>
<?php endif; ?>
<?php
}
I've searched seemingly every relevant question on this but I'm stuck, as none of them address the particular case of uploads through XML RPC.
I want to conditionally change the Wordpress file upload directory, only if the file is coming in through an XML RPC call and only if the call is coming in from a particular user.
My approach is based on a combination of this Answer, this Answer and the Codex.
Here's what I tried with no luck:
add_filter( 'xmlrpc_methods', 'call_intercept1' );
function call_intercept1( $methods ) {
$methods[ 'metaWeblog.newMediaObject' ] = 'custom_upload1';
return $methods;}
function custom_upload1( $args ) {
global $wpdb;
$username = $this->escape( $args[1] );
$password = $this->escape( $args[2] );
$data = $args[3];
$name = sanitize_file_name( $data['name'] );
$type = $data['type'];
$bits = $data['bits'];
if ( !$user = $this->login($username, $password) )
return $this->error;
if ( $username = "XXX" ) {
add_filter('upload_dir', 'custom_upload_dir1');
}
$upload = wp_upload_bits($name, null, $bits);
if ( ! empty($upload['error']) ) {
/* translators: 1: file name, 2: error message */
$errorString = sprintf( __( 'Could not write file %1$s (%2$s).' ), $name, $upload['error'] );
return new IXR_Error( 500, $errorString );
}
return $upload;
}
function custom_upload_dir1( $param ){
$custom_dir = '/the-desired-directory';
$param['path'] = $param['path'] . $custom_dir;
$param['url'] = $param['url'] . $custom_dir;
error_log("path={$param['path']}");
error_log("url={$param['url']}");
error_log("subdir={$param['subdir']}");
error_log("basedir={$param['basedir']}");
error_log("baseurl={$param['baseurl']}");
error_log("error={$param['error']}");
return $param;
}
The file is being uploaded correctly, but the conditional directory change isn't happening.
Does someone know why that would be?
I was able to get this worked out, essentially using Ulf B's Custom Upload Dir as a model and simplifying it from there.
For anyone else facing the same problem, here's what works:
// XMLRPC Conditional Upload Directory
add_action('xmlrpc_call', 'redirect_xmlrpc_call');
function redirect_xmlrpc_call($call){
if($call !== 'metaWeblog.newMediaObject'){return;}
global $wp_xmlrpc_server;
$username = $wp_xmlrpc_server->message->params[1];
$data = $wp_xmlrpc_server->message->params[3];
if($username !== "XXX"){return;}
else {custom_pre_upload($data);}}
function custom_pre_upload($data){
add_filter('upload_dir', 'custom_upload_dir');
return $data;}
function custom_post_upload($fileinfo){
remove_filter('upload_dir', 'custom_upload_dir');
return $fileinfo;}
function custom_upload_dir($path){
if(!empty($path['error'])) { return $path; } //error; do nothing.
$customdir = '/' . 'your-directory-name';
$path['subdir'] = $customdir;
$path['path'] .= $customdir;
$path['url'] .= $customdir;
return $path;}
In my application I need to create a widget in admin dashboard which will display a section of all post_types associated with the number of posts each of them has.
To accomplish the above I added the following code block in my functions.php file:
add_action('wp_dashboard_setup', 'tp_post_counts_reference');
function tp_post_counts_reference() {
global $wp_meta_boxes;
wp_add_dashboard_widget('custom_help_widget', 'Posts in all post types', 'custom_dashboard_help');
}
function custom_dashboard_help() {
$types = get_post_types();
foreach( $types as $type )
{
if($type != 'travelog' && $type != 'package_tour' && $type != 'hotel-info') continue;
$typeobj = get_post_type_object( $type );
echo '' . $typeobj->labels->name . ': ' . wp_count_posts( $type )->publish . '<br />';
}
}
But $typeobj->rewrite['slug'] is actually outputting the post_type rather than its corresponding slug.
For example:
I have the following custom post types
Travelog (name: Travelog, post_type: travelog, slug: travelogs)
Hotel Info (name: Hotel Info, post_type: hotel-info, slug: hotels)
The actual output for
'' . $typeobj->labels->name . ': ' . wp_count_posts( $type )->publish
are
Travelog: 6
and
Hotel: 11
when I expect them to output:
Travelog: 6
and
Hotel: 11
Please tell me what I did wrong :(
NB: My WP version is 4.7.5
Rather than trying to manually build your post type URL, I would recommend that you instead leverage the built-in WordPress function get_post_type_archive_link.
That would look like this:
function custom_dashboard_help() {
$types = get_post_types();
// Alternate method for testing if custom type.
$custom_types = array( 'travelog', 'package_tour', 'hotel-info' );
foreach( $types as $type ) {
// If not a custom post type, don't render the link
if( ! in_array( $type, $custom_types ) ) {
continue;
}
$typeobj = get_post_type_object( $type );
// Use get_post_type_archive_link function to get URL
echo '' . $typeobj->labels->name . ': ' . wp_count_posts( $type )->publish . '<br />';
}
}
Is it possible for a file in the plugin directory to be used as a custom Page Template?
Also, how do you make a plugin create a page?
I'm developing a plugin for a client based on a theme, he wants this plugin to make sales pages while being able to use his theme on the homepage. This is a product that I'm making for him to market so it needs to be automated all through the plugin.
Is this possible?
EDIT
I have the activation/deactivation hooks in my plugins main file, and it's not working. Here's the code:
$filename = __FILE__;
register_activation_hook($filename, 'superActivation');
register_deactivation_hook($filename, 'superDeactivation');
global $myFile; global $fh; global $stringData; global $filename;
$myFile = "testFile.txt";
$stringData = "Testing\n";
$fh = fopen($myFile, 'w') or die("can't open file");
function superActivation() {
global $myFile; global $fh; global $stringData; global $filename;
fwrite($fh, $stringData);
fclose($fh);
}
function superDeactivation() {
$myFile = "testFile.txt";
unlink($myFile);
}
You can do this with the template_redirect hook. Here's my code to manually replace the template for a custom post type with one in the theme if there isn't one in the template folder. Put this in your plugin file and then put a folder underneath your plugin called themefiles with your default theme files.
//Template fallback
add_action("template_redirect", 'my_theme_redirect');
function my_theme_redirect() {
global $wp;
$plugindir = dirname( __FILE__ );
//A Specific Custom Post Type
if ($wp->query_vars["post_type"] == 'product') {
$templatefilename = 'single-product.php';
if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
$return_template = TEMPLATEPATH . '/' . $templatefilename;
} else {
$return_template = $plugindir . '/themefiles/' . $templatefilename;
}
do_theme_redirect($return_template);
//A Custom Taxonomy Page
} elseif ($wp->query_vars["taxonomy"] == 'product_categories') {
$templatefilename = 'taxonomy-product_categories.php';
if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
$return_template = TEMPLATEPATH . '/' . $templatefilename;
} else {
$return_template = $plugindir . '/themefiles/' . $templatefilename;
}
do_theme_redirect($return_template);
//A Simple Page
} elseif ($wp->query_vars["pagename"] == 'somepagename') {
$templatefilename = 'page-somepagename.php';
if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
$return_template = TEMPLATEPATH . '/' . $templatefilename;
} else {
$return_template = $plugindir . '/themefiles/' . $templatefilename;
}
do_theme_redirect($return_template);
}
}
function do_theme_redirect($url) {
global $post, $wp_query;
if (have_posts()) {
include($url);
die();
} else {
$wp_query->is_404 = true;
}
}
You CAN add page templates from a plugin very easily by manipulating the page cache.
To customise, simply edit the following code block within the __construct method;
$this->templates = array(
'goodtobebad-template.php' => 'It\'s Good to Be Bad',
);
This is designed for a plugin (the template files are searched for in the root directory of the plugin). This can be changed if desired - check out my full tutorial http://www.wpexplorer.com/wordpress-page-templates-plugin/ for greater detail on this solution. These files are also in exactly the same format as if they were to be included directly in a theme.
Full code;
class PageTemplater {
/**
* A Unique Identifier
*/
protected $plugin_slug;
/**
* A reference to an instance of this class.
*/
private static $instance;
/**
* The array of templates that this plugin tracks.
*/
protected $templates;
/**
* Returns an instance of this class.
*/
public static function get_instance() {
if( null == self::$instance ) {
self::$instance = new PageTemplater();
}
return self::$instance;
}
/**
* Initializes the plugin by setting filters and administration functions.
*/
private function __construct() {
$this->templates = array();
// Add a filter to the attributes metabox to inject template into the cache.
add_filter(
'page_attributes_dropdown_pages_args',
array( $this, 'register_project_templates' )
);
// Add a filter to the save post to inject out template into the page cache
add_filter(
'wp_insert_post_data',
array( $this, 'register_project_templates' )
);
// Add a filter to the template include to determine if the page has our
// template assigned and return it's path
add_filter(
'template_include',
array( $this, 'view_project_template')
);
// Add your templates to this array.
$this->templates = array(
'goodtobebad-template.php' => 'It\'s Good to Be Bad',
);
}
/**
* Adds our template to the pages cache in order to trick WordPress
* into thinking the template file exists where it doens't really exist.
*
*/
public function register_project_templates( $atts ) {
// Create the key used for the themes cache
$cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
// Retrieve the cache list.
// If it doesn't exist, or it's empty prepare an array
$templates = wp_get_theme()->get_page_templates();
if ( empty( $templates ) ) {
$templates = array();
}
// New cache, therefore remove the old one
wp_cache_delete( $cache_key , 'themes');
// Now add our template to the list of templates by merging our templates
// with the existing templates array from the cache.
$templates = array_merge( $templates, $this->templates );
// Add the modified cache to allow WordPress to pick it up for listing
// available templates
wp_cache_add( $cache_key, $templates, 'themes', 1800 );
return $atts;
}
/**
* Checks if the template is assigned to the page
*/
public function view_project_template( $template ) {
global $post;
if (!isset($this->templates[get_post_meta(
$post->ID, '_wp_page_template', true
)] ) ) {
return $template;
}
$file = plugin_dir_path(__FILE__). get_post_meta(
$post->ID, '_wp_page_template', true
);
// Just to be safe, we check if the file exist first
if( file_exists( $file ) ) {
return $file;
}
else { echo $file; }
return $template;
}
}
add_action( 'plugins_loaded', array( 'PageTemplater', 'get_instance' ) );
Check out my tutorial on this for more info.
http://www.wpexplorer.com/wordpress-page-templates-plugin/
I hope this helps you in what you want to do :)
the code david posted above almost works for me. but it seems to blanket over all posts and pages for me. This code below works great for adding a template to a single post type that is created by my main plugin file
function get_book_post_type_template($single_template) {
global $post;
if ($post->post_type == 'books') {
$single_template = dirname( __FILE__ ) . '/themefiles/single-books.php';
}
return $single_template;
}
add_filter( "single_template", "get_book_post_type_template" ) ;
but I'm having trouble getting it to work with a custom page templates that don't have a post_type or has a post_type = page for instance lets say the custom page is an auxiliary member login page to see my custom posts. in my case this file is called myaccount.php and i've included it in a subfolder within my plugin folder named themefiles.
//Add Page and Post Template Files to Current Theme
add_action("template_redirect", 'my_account_redirect');
function my_account_redirect() {
global $wp;
//Set myAccount Custom Page Template
if (isset($wp->query_vars['pagename'] ) == "myaccount") {
$templatefilename = 'myAccount.php';
if (file_exists(dirname( __FILE__ ) . '/themefiles/' . $templatefilename)) {
$return_template = dirname( __FILE__ ) . '/themefiles/' . $templatefilename;
}
do_account_redirect($return_template);
}
}
//Finishing setting templates
function do_account_redirect($url) {
global $post, $wp_query;
if (have_posts()) {
include($url);
die();
} else {
$wp_query->is_404 = true;
}
}
when i do the above code the myaccount template shows up on all pages except for home which i believe is because it is set to a blogroll instead of a static page
I cannot reply to user1912899, but their recommendation seems to be the most elegant solution. To use a custom template to override single-post.php, I've implemented the following code. This will work for any custom single-****.php file you add to your plugin. If it doesn't exist, it just falls back to what WordPress normally uses.
add_action('template_include', 'my_template_include');
function my_template_include($template) {
$file = dirname( __FILE__ ).'/theme/single-'.get_post_type().'.php';
if(file_exists($file)) {
$template = $file;
}
return $template;
}