I am searching a solution to add a custom template for plugin on short code. But I am unable to do it successfully.
I have made a template folder in my plugins folder and put a custom template in it. I want to show this template by putting a short code. For this I have written following piece of code.
function wp_parse_login()
{
add_action('template_redirect', 'my_template');
function my_template()
{
include ('template/login.php');
exit;
}
}
add_shortcode('parse_login_page','wp_parse_login');
but it's not working. I have include this file to my main plugin file. I think I am leaving some hooks.
Here an adapted example from WordPress Codex
function wp_parse_login() {
ob_start();
include ('template/login.php');
return ob_get_clean();
}
add_shortcode('parse_login_page','wp_parse_login');
You first set a page template in DB Like:
$table_post_meta = $wpdb->prefix.'postmeta';
$meta_data = array(
'post_id' => $post_id, (Get dynamically post id of a page)
'meta_key' => '_wp_page_template',
'meta_value' => 'template/login.php' (Give the file path)
);
$wpdb->insert($table_post_meta,$meta_data) or die(mysql_error());
The you write in your this code in your template page:
function wp_parse_login() {
ob_start();
include ('template/login.php');
return ob_get_clean();
}
add_shortcode('parse_login_page','wp_parse_login');
Hope you find your solution.
Related
I am migrating and troubleshooting a wordpress theme. I have a gutenberg block and an alert module set up to use the Timber composer package that allows the use of the twig templating engine.
I have it configured in a class
ProcessorTable.php
<?php
namespace CRG\Blocks;
class ProcessorTable
{
public function __construct()
{
$this->createProcessorTable();
}
public function createProcessorTable()
{
if (function_exists('acf_register_block')) {
// register a custom vue gravity forms block
acf_register_block(array(
'name' => 'processor-table-block',
'title' => __('Processor Table Block'),
'description' => __('A Block for displaying a contracted database processors table'),
'category' => 'crg-custom-blocks',
'icon' => 'welcome-write-blog',
'render_callback' => array($this, 'render_processor_table'),
'keywords' => array( 'table' ),
));
}
}
public function render_processor_table($block, $content = '', $is_preview = false)
{
$context = \Timber\Timber::context();
// Store block values.
$context['block'] = $block
// Store field values.
$context['fields'] = get_fields();
// Store $is_preview value.
$context['is_preview'] = $is_preview;
$twigPath = TEMPLATEPATH . "/src/views/blocks/block-processor-table.html.twig";
\Timber\Timber::render($twigPath, $context, 600);
}
}
AlertNotification.php
<?php
namespace CRG\Controllers\SiteWide;
class AlertNotification {
public function AlertModal(){
$context = \Timber\Timber::context();
$context['alert_header_text'] = get_field('alert_header_text', 'options');
$context['alert_text'] = get_field('alert_text', 'options');
$context['alert_icon'] = get_field('alert_icon', 'options');
$context['alert_color'] = get_field('alert_color', 'options');
$context['alert_toggle'] = get_field('alert_toggle', 'options');
\Timber\Timber::render( TEMPLATEPATH . "/src/views/sitewide/alert-modal.html.twig", $context );
}
}
this code worked on a cpanel server and on nexcess managed wordpress hosting, but when I migrated it to kinsta the code stopped rendering. It looks like the Timber::context() works, and the method can find the html.twig files, but it can't render the twig template, and produces no errors
I have tried troubleshooting this by checking the composer package versions, reinstalling the timber composer package, testing the code outside of the class directly into the functions.php file, and verifying the code can reach and output the file contents as a string. I checked the error log files and was unable to find a solution, or a cause of the error.
Hosting configuration:
Kinsta Caching: disabled
Wordpress debugging is enabled
Running on PHP 7.4
Using MySQL
I added this to my functions.php file
\Timber\Timber::$locations = TEMPLATEPATH . "/src/views";
so everything in the functions.php file for timber to work would look like this
require_once(__DIR__ . '/vendor/autoload.php');
$timber = new Timber\Timber();
\Timber\Timber::$locations = TEMPLATEPATH . "/src/views";
then in my ProcessorTable.php file I can call the Twig file path like this
$twigPath = "/blocks/block-processor-table.html.twig";
return \Timber\Timber::render( $twigPath, $context);
My guess about what went wrong is that the timber package was looking in the wrong director when rendering, based on incorrect locations information the Timber:locations method allows you to set up a custom location to store the templates: https://timber.github.io/docs/guides/template-locations/
Not sure why this would have worked on other systems, but not Kinsta. However, this appears to be a good practice going forward by explicitly setting the views.
I created my prestashop module, with a hook to display my specific search form.
public function hookDisplayTopColumn($params)
{
$this->context->controller->addCSS($this->_path.'css/modelfilter.css', 'all');
$this->context->controller->addJS($this->_path.'js/modelfilter.js');
$marque = $this->getSubCategories($this->marquesCategory);
$this->context->smarty->assign(array(
'marques' => $marque,
));
return $this->display(__FILE__, 'form_model.tpl');
}
JS and CSS files are not included. To find why, I added a parse line in classes/controller/FrontController.php :
public function addMedia($media_uri, $css_media_type = null, $offset = null, $remove = false, $check_path = true)
{
echo 'addMedia '.$media_uri."<br/>\n";
And the result is : all css/js files appears before the beginning of the page (just after <body>), but my files comes just before displaying form_model.tpl
Please, how to make my files to be called in the good time ?
You shall not use $this->context->controller->addCSS and addJS outside of hookDisplayHeader().
When the header of your page is built hookDisplayHeader() is called to add headers elements. Once this hook is done, the header is built and can't be changed.
So when the hook hookDisplayTopColumn() is called, the header is already built.
To add your files you have to implement the hookDisplayHeader() in your module:
public function hookDisplayHeader($params)
{
$this->context->controller->addCSS($this->_path.'css/modelfilter.css', 'all');
$this->context->controller->addJS($this->_path.'js/modelfilter.js');
}
And remove those lines from hookDisplayTopColumn().
I'm trying to change the og:url content on specific posts but I am unsure how to implement my changes from the functions.php file.
I have tried doing this using the content I have found on the internet but believe it has been updated since then.
I have updated the class-opengraph.php file in the the wordress-seo plugin folder which works, please find my edits below:
public function url() {
$url = apply_filters('wpseo_opengraph_url',
WPSEO_Frontend::get_instance()->canonical(false));
if (is_string($url) && $url !== '' ) {
if (is_page(32721)) {
$this->og_tag('og:url', esc_url('testing'));
} else {
$this->og_tag( 'og:url', esc_url( $url ) );
}
return true;
}
return false;
}
It's not good to modify the plugin files directly because when you update the plugin, you will lose all your changes to those files.
There are two solutions that I have found to do such thing.
You get the instance of the class WPSEO_Frontend, then update it's options for the og:url.
e.g.
$object = WPSEO_Frontend::get_instance();
$object->options['og_url'] = esc_url( $url );
This can be added before the wp_head()
You can use add_filter to hook a function to the filter action. We use the filter action below.
Filter: 'wpseo_opengraph_url' - Allow changing the OpenGraph URL
e.g.
function update_og_url($url) {
return "http://www.yoursampleurl.com";
}
add_filter('wpseo_opengraph_url', 'update_og_url', 10, 1);
Source: Wordpress SEO API
I have a question regarding wp_rewrite, It doesn't display correctly,
I need to make this url
eduedu/wp-content/plugins/workwork/admin/templates/tcpdf/samp/flash.php
to
eduedu/generator
It works when using .htaccess, the problem is, that It doesnt work when place inside the plugin folder, the htaccess must be place in the root folder of wordpress. So I thought of using wp_rewrite.
Here is my code, I added this on the page when in it will redirect to
eduedu/wp-content/plugins/workwork/admin/templates/tcpdf/samp/flash.php
I'm not sure if its correct, any idea?
add_action('generate_rewrite_rules', 'cs_rewrite_rules');
add_filter('init', 'eduFlush');
function cs_rewrite_rules() {
global $wp_rewrite;
$new_non_wp_rules = array(
'^generator/?$' => 'eduedu/wp-content/plugins/workwork/admin/templates/tcpdf/samp/flash.php',
);
$wp_rewrite->non_wp_rules += $new_non_wp_rules;
}
function eduFlush(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
Maybe something along these lines would help:
function site_router() {
global $url_array;
$url_array = explode("/",$_SERVER['REQUEST_URI']);
$route = $url_array[1];
$template_dir = 'wp-content/plugins/workwork/admin/templates/tcpdf';
switch($route) {
case 'generator':
load_template($template_dir.'/samp/flash.php');
die();
break;
}
}
add_action( 'send_headers', 'site_router');
It gets the current URL array (domain.com/url_array[1]), then it gets where you want to get the file from.
The switch gets the url, so domain.com/generator, and then loads in the template from the file you call.
You may need to change the url_array[1] to [2] if wordpress is in a sub-folder (domain.com/wp_root)
My website having feature requirement of blogging. I have to make blog which would look same like my website appearance.
How to combine CodeIgniter and Wordpress blogging(only) functionality such that it should look like within same website?
I have seen this question: Wordpress template with codeigniter. But didn't got much clue.
Seems like a bit of overkill.
Why not use a Restful service like json_api to retrieve your posts, then copy over the css file(parts)?
You do this you will need to create 2 files and modify 2 existing functions. One function is in CodeIgniter and the other is in Wordpress.
Here are the steps.
1.) Open your configs/hooks.php file and create a pre_controller hook as follows:
$hook['pre_controller'] = array(
'class' => '',
'function' => 'wp_init',
'filename' => 'wordpress_helper.php',
'filepath' => 'helpers'
);
2.) Create a new file in your helpers directory called 'wordpress_helper.php', and add the following code to it:
/**
*
*/
function wp_init(){
$CI =& get_instance();
$do_blog = TRUE; // this can be a function call to determine whether to load CI or WP
/* here we check whether to do the blog and also we make sure this is a
front-end index call so it does not interfere with other CI modules.
*/
if($do_blog
&& ($CI->router->class == "index" && $CI->router->method == "index")
)
{
// these Wordpress variables need to be globalized because this is a function here eh!
global $post, $q_config, $wp;
global $wp_rewrite, $wp_query, $wp_the_query;
global $allowedentitynames;
global $qs_openssl_functions_used; // this one is needed for qtranslate
// this can be used to help run CI code within Wordpress.
define("CIWORDPRESSED", TRUE);
require_once './wp-load.php';
define('WP_USE_THEMES', true);
// Loads the WordPress Environment and Template
require('./wp-blog-header.php');
// were done. No need to load any more CI stuff.
die();
}
}
3.) Open wp-includes/link-template.php and made the following edit:
if ( ! function_exists('site_url'))
{
function site_url( $path = '', $scheme = null ) {
return get_site_url( null, $path, $scheme );
}
}
4.) Copy url_helper.php from the CodeIgniter helper folder to your APPPATH helper folder
and make the following edit:
if ( ! function_exists('site_url'))
{
function site_url($uri = '', $scheme = null)
{
// if we are in wordpress mode, do the wordpress thingy
if(defined('CIWORDPRESSED') && CIWORDPRESSED){
return get_site_url( null, $path, $scheme );
}else{
$CI =& get_instance();
return $CI->config->site_url($uri);
}
}
}
The steps above will allow you to dynamically load either your CI app or your WP site based on some simple filtering. It also gives you access to all CI functionality within WP of that is something you can use.