I am trying to pass URL parameters to a Wordpress template page:
www.mysite.com/my_wp_page_template/?city=Anytown&state=ST
Based on some forum examples I have added the following code to the functions.php:
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'City';
$qvars[] .= 'State';
return $qvars;
}
And, I am trying to extract the variables in a PHP file that is included in a WP page using:
report_for_city.php
<?php
$strCity = $_GET["City"];
$strState = $_GET["State"];
$strCityState = $strCity . ' ' . $strState;
?>
<p>This page contains the report for <?php $strCityState ?>.</p>
But, the variable is not getting inserted in the output HTML. The HTML is being inserted into the WP page, so the PHP template is getting invoked from the WP page using the insert-php plugin:
[insert_php]include('wp-content/php/report_for_city.php');[/insert_php]
What am I doing wrong? How can this be fixed?
The PHP code was missing echo in:
php $strCityState
The following code corrected the problem:
php echo $strCityState
Is there an easier way to output the value than this?
Related
When I try to display a shortcode in pages it shows this 'Updating failed. The response is not a valid JSON response' error but the page gets updated and no error shows in the frontend. When I use the classic editor it works fine but issues with block editor.
Here its code
<?php
class EmailPluginfront {
function __construct() {
add_shortcode('email', array($this,'front_display'));
}
function front_display() {
echo '<h2 style="text-align:center; margin-bottom:30px">'.get_option('email_title_field').'</h2>';
}
}
$EmailPluginfront = new EmailPluginfront();
you need to return data at the end of your function. try using this in your shortcode function
ob_start();
echo("Hello there!"); // all your code here
$output = ob_get_contents();
ob_end_clean();
return $output
Can you please use 'return' instead of the 'echo'?
If I try to use ACF inside my custom functions plugin I get this error:
Uncaught Error: Call to undefined function get_field() in
I created my custom function plugin to avoid using the default functions.php that comes with the theme, because it overrides on every update.
My code:
<?php
$author_id = get_the_author_meta('ID');
$author_badge = get_field('post_autor', 'user_'. $author_id );
?>
<img src="<?php echo $author_badge['url']; ?>" alt="<?php echo $author_badge['alt']; ?>" />
I'm using a Gantry5 theme.
I already followed the documentation and read some posts with examples, but even so I’m not being able to get it working. The field is set as User Object.
UPDATE:
I found this code in the developer's website, I'm going to be giving it a try. This code supposed to allow ACF (free version) to be loaded inside a plugin.
// Define path and URL to the ACF plugin.
define( 'MY_ACF_PATH', get_stylesheet_directory() . '/includes/acf/' );
define( 'MY_ACF_URL', get_stylesheet_directory_uri() . '/includes/acf/' );
// Include the ACF plugin.
include_once( MY_ACF_PATH . 'acf.php' );
// Customize the url setting to fix incorrect asset URLs.
add_filter('acf/settings/url', 'my_acf_settings_url');
function my_acf_settings_url( $url ) {
return MY_ACF_URL;
}
// (Optional) Hide the ACF admin menu item.
add_filter('acf/settings/show_admin', 'my_acf_settings_show_admin');
function my_acf_settings_show_admin( $show_admin ) {
return false;
}
https://www.advancedcustomfields.com/resources/including-acf-within-a-plugin-or-theme/
Second Update:
Still having the same problem.
Suppose I have a clean wordpress install, with a basic custom theme.
In that theme, I have a custom page template which is just an iframe, which is pointed at a webapp on a different domain.
So suppose my wordpress install can be reached at http://example.com, and my page with the iframe template is located at http://example.com/members/.
I now want to add dynamic routes, so that all requests to http://example.com/members/login, or http://example.com/members/event/1 (for example) all go to http://example.com/members/ but pass the second part of the route ('/login', or '/event/1') to the iframe inside.
What would be the best way to accomplish this, without having to hack into Wordpress' internals?
I found this plugin: https://wordpress.org/plugins/wp-on-routes/ but much to my dismay I discovered that when I tried using it it completely overwrites Wordpress' built in routing, which meant I would have to manually re-add each and every URL (as I understand it, I'm not that accomplished in PHP), which is a no go as my client still needs to be able to post without manually editing php files.
Thank you for reading.
You can add routing using the add_rewrite_rule hook like so:
function custom_rewrite_rule() {
add_rewrite_rule('members/([^/]+)/([^/]+)/?$',
'index.php?memberspage=$matches[1]&event_id=$matches[2]',
'top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
You may need to create several depending on the URLs you rewriting. You can then use the URL parameters in your template to load the appropriate page in your iframe.
I managed to find a solution for my problem, thanks to Fencer04's suggestion. I found this page: https://developer.wordpress.org/reference/functions/add_rewrite_rule/ where I found an example that was close enough to my problem to work.
So in functions.php:
function custom_rewrite_rule(){
$page_id = 318; // replace this ID with the page with the iFrame template
$page_data = get_post($page_id);
if(!is_object($page_data)){
return; // all other pages don't have to support custom deeplinks
}
// catches deeplinks 1 level deep, i.e.: /members/profile
add_rewrite_rule(
$page_data->post_name . '/([^/]+)/?$',
'index.php?pagename=' . $page_data->post_name . '&memberspage=$matches[1]',
'top'
);
// catches deeplinks 2 levels deep, i.e.: /members/profile/edit
add_rewrite_rule(
$page_data->post_name . '/([^/]+)/([^/]+)/?$',
'index.php?pagename=' . $page_data->post_name . '&memberspage=$matches[1]&members_param=$matches[2]',
'top'
);
// catches 3 levels deep, i.e. /members/profile/edit/confirm
add_rewrite_rule(
$page_data->post_name . '/([^/]+)/([^/]+)/([^/]+)/?$',
'index.php?pagename=' . $page_data->post_name . '&memberspage=$matches[1]&members_param=$matches[2]&members_param2=$matches[3]',
'top'
);
}
add_action('init', custom_rewrite_rule);
Next I added filters for the new query_vars:
add_filter('query_vars', function($vars) {
$vars[] = "memberspage";
$vars[] = "members_param";
$vars[] = "members_param2";
return $vars;
});
And then in my template-iframe.php, I can access these parameters like so:
<?php
// get query strings
global $wp_query;
$page = $wp_query->query_vars['memberspage'];
$params = $wp_query->query_vars['members_param'];
$params2 = $wp_query->query_vars['members_param2'];
$membersBaseURL = 'http://members.domain.com/';
$iframeURL = $membersBaseURL;
if(isset($page)){
$iframeURL = $iframeURL . $page . '/';
}
if(isset($params)){
$iframeURL = $iframeURL . $params . '/';
}
if(isset($params2)){
$iframeURL = $iframeURL . $params2 . '/';
}
?>
<iframe id="iframeLeden" src="<?php echo($iframeURL) ?>" frameborder="0"></iframe>
So now if I go to http://www.domain.com/members/login, it'll show me the correct static WP page, with inside an iframe that shows the page http://members.domain.com/login/ .
I'm trying to run a loop on WordPress that returns user information. When I have the PHP code to echo the results in the same file as the query, it works as expected. However, if I use include to access a transformation held elsewhere, it doesn't return any information, even if the code in the included file is identical. Why would this be?
My loop is below:
<?php
// The Query
$user_query = new WP_User_Query( array( 'include' => array( 1, 2, 3 ) ) );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
?>
<?php transform('user_directory'); echo $user->display_name; ?>
<?php
}
} else {
echo 'No users found.';
}
?>
Transform is a simple function that includes another page by name:
function transform($transformation) {
include 'transformations/'.$transformation.'.php';
}
In this case, the echo command works, whereas the included file (via Transform) doesn't. The included file is definitely being found and is rendering other html and PHP code perfectly, it just doesn't seem able to find anything under $user.
The code held in the included PHP file is:
<?php echo $user->display_name; //Won't return this text
echo "It returns this text fine"; ?>
Am I not able to access an array created on one page with included code from another?
The problem is, you are including it from a function.
A function, in PHP, does have it's own scope.
The included file will inherit the, empty, scope of the function.
You could pass the $user to the function:
function transform($transformation, $user) {
include 'transformations/'.$transformation.'.php';
}
...
<?php transform('user_directory', $user); echo $user->display_name; ?>
This will set the $user variable, and make it accessible by the included file.
Hope this helps :-)
I'd like to get the current url in a .tpl file in OpenCart.
I've seen this answer but it works only in a php file.
I have to get this way:
_my_array.push(['_productName',"<?php echo $heading_title; ?>"]);
**_my_array.push(['_productUrl', ["how can I get url ?"]]);**
Thanks
To get full url
<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>
and btw those answers work on tpl files also because tpl files are sort of php files only
The best practice would be to get it in the controller then use it in the view file.
In your controller
$data['current'] = $this->url->link($this->request->get['route'], '', 'SSL');
Then in the view file
echo $current;
I also needed current url for schema.org.
Generally you can create current url via link function
public function link($route, $args = '', $secure = false)
So https link for product page that would be
$data['share'] = $this->url->link('product/product', 'product_id=' . (int)$this->request->get['product_id'], true);
Then in the view file
echo $share