Allow PHP codes in Wordpress posts & pages (Using functions.php) - php

I need to write pure PHP codes in WP posts - not using a plugin or snippets etc.
I know this Q has been asked before, but all were answered by using plugins etc.
There were some plugins like PHP-Exec that could do the job, but due to latest WP/PHP updates they no longer work.
Is there any way to write some functions in FUNCTIONS.PHP to allow this?
So a code like this can work as a WP post?
<p>
My text paragraph.
</p>
<?php
// Custom PHP codes - vary in wp posts
echo "Anything ... ";
?>
<p>
My second text paragraphs.
</p>
Thanks a lot guys!

Best approach would be to wrap your PHP code in a shortcode inside functions.php and then call it out of your page.
function anything_function() {
// your actual functionality.
$message = "Anything ... ";
return $message;
}
// register shortcode
add_shortcode('anything', 'anything_function');
And then you can use the [anything] shortcode inside your templates.
Please refer to Shortcode API for more information.

Related

How to add plugins to my custom wordpress theme?

I'm making a WordPress theme by myself since I'm working for the first time in Wordpress I've watched some tutorials about it.
I have page.php header and footer and ofc an index. I insert the content from the pages with this:
<?php echo get_post_field('post_content', $post->ID); ?>
but I tried the get_post in a while loop with same result..
Everything is fine but when I want to use a plugin I can't add to my page... When I insert the shortcode of it it shows only the shortcode string... There are some plugins where I can click a "view" option and it would show a page with my plugin (for example a calendar) but that page is empty...
When I activate an original theme it works instantly... So I'm sure something is missing from my theme something which can load the plugins but I couldn't find solution for it.
Any ideas?
Did you add the <?php wp_head(); ?> function before the head area of the html document is closing? It imports important scripts and styles from wordpress itself (and probably also from the plugins).
See here:
https://developer.wordpress.org/reference/functions/wp_head/
Before closing the body area, the template should also include
<?php wp_footer();?>
See here:
https://developer.wordpress.org/reference/functions/wp_footer/

how to automate the process of adding a link to comment in wordpress every time a comment is created

so we are making a wordpress site that automatically adds a link to a comment every time a comment is created. The theme we are using is html5 blank. I have written several lines of code into its function.php. however it is not working right now, here is what i wrote:
function bo_wrap_comment_text($content) {
$content = get_comment_text();
$texta = urlencode(get_comment_text());
$textb = "http://www.google.com/search?btnI&q=" + $texta;
return ''.$content.'';
}
add_filter('wp_insert_comment','bo_wrap_comment_text',1000);
a separate code(this one works) is tested here: http://phpfiddle.org and the code is:
<?php
$texta='i hate apple';
$textb=urlencode($texta);
$textc= "http://www.google.com/search?btnI&q=".$textb;
echo ''.$texta.'';
?>
the wordpress site is here. It is simply a static front page. the title you see there is the title of comment area. everything related with blog posts has been hidden using css. we have manually added links to several comments as prototyping. what we want is replace manual work with wordpress functions. I would really appreciate any help.
You need this hook instead to edit comment data:
http://codex.wordpress.org/Plugin_API/Filter_Reference/preprocess_comment

WP Jetpack publicize insert default text(s)

How can I place a default text (hashtag) in the Custom Message?
The textarea is (located in line 643) under jetpack/modules/publicize/ui.php
I tried to put the text in front of $title in various ways, like:
<?php echo "#myhashtag $title"; ?>
or
<?php echo '#myhashtag '.$title; ?>
but it just echoes the text, not the $title.
Any ideas will be greatly appreciated.
You can use the approach of this Wordpress plugin i made (Publicize With Hashtags), which does exactly that. It basically use and action trigger bound to the 'save_post' native event.
If you want to develop your own one you can have a look at my Source Code on the project's GitHub page or in this installation & usage guide I wrote about it.
You can add a filter, like so, to your theme's functions.php or a site-specific plugin:
add_filter( 'wpas_default_prefix', 'add_default_publicize_hashtag_prefix', 10, 4 );
function add_default_publicize_hashtag_prefix() {
$default_tags = '#yourhastaghere ';
return $default_tags;
}
This will add your default hashtag before your title without you needing to hack the WordPress core.
jetpack/modules/publicize/ui.php itself states in its comments:
/**
* Only user facing pieces of Publicize are found here.
*/
You added your hashtag in the textarea which allows admins to enter a custom message (click edit and it will slide down with your hashtag).
As #Yazmin mentioned, the best way to permanently edit the message is using a filter. The filters available are wpas_default_prefix, wpas_default_message, and wpas_default_suffix.
Personally, I had no success using these filters and I'm interested in a working solution to this issue myself.

Changing woo commerce HTML that gets outputted with do_action

I'm trying to build a webshop with woo commerce which seems like a very nice and easy to use system. But I'm really having some trouble with it. A lot of times there's a function like this in the code: do_action( 'woocommerce_checkout_order_review' ) which outputs some HTML that I really want to change. The documentation that is provided isn't making me any wiser. How can I change this HTML. I would really appreciate any help trying to understand how this works!
The answer to the question, how do I modify the content ouputted in woocommerce_checkout_order_review ?
The answer:
The HTML is located in the template file here:
wp-content/plugins/woocommerce/templates/checkout/review-order.php
You can easily modify this content in a plugin or theme. Read more about that here: http://docs.woothemes.com/document/template-structure/
do_action( 'woocommerce_checkout_order_review' )
this is action declared somewhere, this is done so that whenever you want to add anything for example say "hello there", you dont have to hardcode it .. you can simply do it this way ..
function add_something() {
echo 'hello there';
}
add_action('woocommerce_checkout_order_review','add_something');
Refer here
Add Action
This would add "hello there" in the rendered html page..exactly where you have do_action('woocommerce_checkout_order_review'); in the code . keep this snippet in your functions.php and play with it ;)

wordpress post/page content in code

i am trying to insert a post/page into one of my themes files and it wont display shortcodes or php
i have created a page called home in the wordpress admin-paned and inserted into my code the following:
<div id="home_page"> <!-- echos the content from the page "home" id:43 -->
<?php $home_id = 43;
$home_page = get_page( $home_id );
?>
<?php echo $home_page->post_content; ?>
</div> <!-- end #home_page -->
and non of the shortcodes that i have in the page work.
i installed a php in post or page and tried useing php and it doesnt work.
when i insert
echo do_shortcode('[youtube_sc url=http://www.youtube.com/watch?v=Db3XGpt6nNU]');
directly into the code it works.
does anyone know y this happens?
thank you.
I got an answer in wordpress.stackexchange.com
I Quote:
You need to apply the filter the_content e.g.:
<?php echo apply_filters('the_content',$home_page->post_content); ?>
Also, you don't need custom shortcodes for youtube, just put the URL in the content (but not a hyperlink), and it'll be swapped out for a youtube player at runtime. No plugins or extra code needed thanks to oembed.
Thank You Tom J Nowell
The $home_page->post_content value is the exact post content as stored in the database. Echoing this does not give any of your shortcodes a chance to run.
You should use a "WordPress Loop" to display the content, as this sets things up to allow you to use template tags such as the_title() and the_content() - this will call the processing functions for shortcodes and other functions like wpautop() that "massage" post content for output.
If you don't want to use a Loop, you could output the content using
echo do_shortcode($home_page->post_content);
This will run the post content through the shortcode processor, giving the shortcodes a chance to run.
For more on how WordPress "massages" post content, you can look here: http://codex.wordpress.org/How_WordPress_Processes_Post_Content

Categories