Add php element to custom page in Wordpress - wordpress

I'm trying to customize a wordpress page to include an iframe which give the users a link to there download. We're using wordpress 2.9.2 with the Thesis theme 1.51. I've been trying to use thesis hooks but appears that the php is stripped from the output. Help? Suggested alternatives?
Code from custom_functions.php:
function add_ejunkie_download_link () {
is_page('slug-url-of-page') {
?>
<?php
echo '<iframe src="https://www.e-junkie.com/ecom/rp.php?noredirect=true&client_id=CID&txn_id=' . htmlspecialchars($_GET["txn_id"]) . '" width="100%" frameborder="0" height="50px"></iframe>';
?>
<?php
}
}
remove_action('thesis_hook_custom_template', 'thesis_hook_custom_template');
add_action('thesis_hook_custom_template', 'add_ejunkie_download_link');

Though not as elegant as custom hook in custom_functions.php, Thesis Open Hook WordPress › Thesis OpenHook « WordPress Plugins is an easy way to add hooks with executable code in them.

Why the remove_action call? I really don't think you need it.
The PHP can't be stripped from the output, because it's just that... PHP. It's parsed at runtime, so it's not stripped, it's executed.
I'm guessing you just want to print the iframe when Thesis calls the thesis_hook_custom_template hook?
Have you double checked this hook is actually getting called, and that it's getting called where you expect it to?
Then try simplifying your hooked function with this;
function add_ejunkie_download_link() {
if (is_page('slug-url-of-page')):
?>
<iframe src="https://www.e-junkie.com/ecom/rp.php?noredirect=true&client_id=CID&txn_id=' . htmlspecialchars($_GET["txn_id"]) . '" width="100%" frameborder="0" height="50px"></iframe>
<?php
endif;
}

Related

Custom Wordpress Gutenberg block not rendering on front end

I have written a custom Wordpress Gutenberg block but it is not showing up on the front end.
I can edit the page and save. The data is successfully stored in the database.
However, when I refresh the front end the output does not show.
I am using PHP to output the data:
function theHtml($attributes)
{
if (!is_admin()) {
wp_enqueue_script("spacerPanelScripts", plugin_dir_url(__FILE__) . 'build/front-end.js', array("wp-element"), '1.0', true);
}
ob_start(); ?>
<pre style="display: none;">
<?php
echo wp_json_encode($attributes);
?>
</pre>
</div>
<?php return ob_get_clean();
}
But when I check the source code of the front-end, I can't see any pre tags being output for this block.
Just for clarity, I have other custom blocks that are working just fine: outputting the pre tag with data, and rendering on the page.
I don't know why this is happening and I have no idea how to go about diagnosing this (I'm new to WP and custom blocks). All I can say is that I have created this block in exactly the same way I have created my other custom blocks and I'm not getting any errors.
Any help with this will be much appreciated.

Wordpress Shortcode: Causing page editor to autoload preview

Very odd issue I am having. I add the following code to just add a test shortcode to my Wordpress functions.php.
//TEST SHORTCODE
add_shortcode( 'sc_brandon_test', 'brandon_test' );
function brandon_test(){
echo 'Brandon\'s Test Shortcode file works correctly!';
}
When using the shortcode [sc_brandon_test] in the editor the page will auto-redirect to a broken preview of the page I am trying to edit.
Any thoughts would be very appreciated.
Usually you don't echo your shortcode output directly but use a return.
echo would output your content immediately.
The desired behaviour is to parse the shortcode output within your main content loop e.g calling the_content() or
apply_filters('the_content', get_post_field('post_content', $post_id));
add_shortcode( 'sc_brandon_test', 'brandon_test' );
function brandon_test(){
$output = 'Brandon\'s Test Shortcode file works correctly!';
return $output;
}
I recommend these guides
WordPress Scholar: WordPress Shortcodes
smashing magazine: WordPress Shortcodes: A Complete Guide

Using wordpress Api for wordpress features

I am building an external website for a client using the bootstrap 4 framework. and they have a Wordpress with all of their data linked, music and videos, etc stored in it. The Wordpress acts as just a management system for their content. I will be building a separate webpage/site for them to present to their users that will allow users to sign up and subscribe for the mailing list and updates etc.... SO having to never use an API ever. I was wondering what do I need to place in the index.php file to call upon the wordpress api from https://wp.example.com. Does it need to go into the head section or right before the closing body tag?
I would like to use the WordPress Api to GET or echo the current user's avatar and username. to include inside the bootstrap header navbar.
<?php
global $current_user;
get_currentuserinfo();
echo get_avatar( $current_user->ID, 64 );
?>
<?php global $current_user; wp_get_current_user(); ?>
<?php if ( is_user_logged_in() ) {
echo 'Welcome Back, ' . $current_user->user_login . "\n"; echo 'User display name: ' . $current_user->display_name . "\n"; }
else { wp_loginout(); } ?>
and finally last but not least I would like to use the wordpress api to check if a user is logged in/out to display/hide a bootstrap button the will contain an exclusive menu. I'm pretty new to this but I think its something like this
<?php global
if ( is_user_logged_in() ) {
// <button type="button" class="btn btn-outline-primary">Menu</button>
} else {
<button type="button" class="btn btn-outline-primary">SignUp/Login</button>
}
?>
BUt I Dont know exactly how to call or request, head or include or whatever it is that is required to use the api from the https://wp.example.com installation in the html or where to place it ie. , , to make these functions work. Please Help
Definitely follow cabrerahector's advice and look at the documentation for the WordPress API...that is the best place to start.
To answer your question...the only reason to use the WP REST API is when you do not want to use WordPress for handling the front-end of your website.
So... you will not be using your 'Theme' folder except for index.php, functions.php and style.css.
You will also probably want to put your WordPress install in a sub-domain and run your front-end from the primary domain.
I put the following code in my theme's index.php
<script type="text/javascript">window.location = 'http://admin.example.com/wp-admin/';</script>
check out this article Using WordPress as a Headless CMS
Also, the WP REST API is limited...I have had to add several custom endpoints because some data is not included out-of-the-box.
You can get 'user' data by using the following endpoint
GET /wp/v2/users/<id>
REST API Handbook/users

Basic Fishpig Wordpress Integration - Display Custom Post on Homepage

I'm trying to create a slider on the homepage of my Magento site. I am totally new to Magento and have someone else on our team coding most of that stuff after realizing how far into the deep end I jumped.
My issue: I'm trying to pull custom posts from WP (with the paid advanced custom fields extension) to display an image that will go into a slider.
I'm stuck at the most basic part - pulling in a list of Wordpress posts.
I created a new file: mytemplatedirectory/default/template/home/slider.phtml with
<?php $posts = $this->getPosts() ?>
<?php foreach ($posts as $_post) : ?>
<?php echo $post->getPostContent() ?>
<?php endforeach ?>
and I put this into the CMS page in the Magento admin:
{{block type="core/template" template="home/slider.phtml"}}
But not even the default post is showing up.
If anyone has any guidance that would be extremely helpful. The beginning steps are what are throwing me off but it would also be nice to have help pulling the custom post and the advanced custom field (although it seems that Fishpig's documentation makes this pretty simple).
Thanks in advance! Sorry for such an amateur question.
The block type you're using does not include the getPosts() method, which is the reason your call to this returns nothing. If you change the block type to 'wordpress/sidebar_widget_posts' then the call to getPosts will return a post collection object.
The following link explains a little bit more about how to include this block and what you can do with it:
Display WordPress Blog Posts on the Magento homepage
Figured this out with Ben's help (who I believe is the creator of the excellent Fishpig extension).
I created a custom post (with the Custom Post Type UI plugin for WP) and a custom field (with the Advanced Custom Fields plugin for WP).
On my Homepage in the CMS I added in the content area
{{block type="wordpress/sidebar_widget_posts" name="wordpress.widget.recent_posts" post_count="5" post_type="slider_home" template="wordpress/sidebar/widget/slider_home.phtml"}}
In that block, slider_home is my post type and slider_home.phtml is a new file I created that pulls the code from wordpress/sidebar/widget/posts.phtml but customizes it to my need.
Within the loop in slider_home.phtml I took out what was currently there and added:
<?php $image = $post->getMetaValue('image'); ?>
<?php $url = $post->getMetaValue('url'); ?>
<a href="<?php echo $url; ?>" target="_blank">
<img src="<?php echo $image; ?>" />
</a>
which is pulling in the custom fields I made in Wordpress. works perfectly and now my client will be able to update their Magento site through the Wordpress CMS.

Wordpress Title Inside Header

I'm tyring to change how the title works within the header.php file on Wordpress. At the moment I have it so it will display out the name of each page the user is on, which works well, apart from on pages that load blog posts.
I'd like it set so the "title" element within the header.php file can cope with custom names too i.e. instead of loading the name of the latest blog as the title when you visit the blog page I want it to just say Blog.
I decided to create my own variable and then using header check to see if that has been set, but I can't get it to work, placing it either before or after the call to the header file.
index.php
$header_title = "Blog";
get_header(); ?>
Then inside the header.php file I have the following code.
<title>
April Kelley |
<?php if (isset($header_title)) {
echo $header_title;
} else {
the_title();
}?>
Now to my mind this should work, so where am I going wrong?
PS. I only intend to use this $header_title variable on certain pages like index.php and seach.php where the pages pull in the blog posts themselves, so will isset still return false is the variable cannot be found?
A proper way of doing this is to use the_title filter, you don't need to modify template, e.g. :
function my_title($title, $id) {
if (is_home())
$title.= ' | Blog';
return $title;
}
add_filter('the_title', 'my_title');
If you really want to use your own var, you should read this : How to use own php variables in wordpress template?
replace
the_title();
as
echo get_the_title()
hope this work for you

Resources