Can someone please give me a hello world example for placing text in a wordpress content page through a custom php plugin?
I have been doing it through creating pages with a div id (through wp-admin) and loading content through extensive amounts AJAX/Javascript into that same id. I want to learn how to do this server side. ( It would make my life soooo much easier )
Use the Exec-PHP plug-in: http://wordpress.org/extend/plugins/exec-php/
Are you sure you want to use a plugin? Placing shorcodes in the functions.php file is the easiest route.
Example
In your functions.php file
function caption_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'class' => 'caption',
), $atts ) );
return '<span class="' . esc_attr($class) . '">' . $content . '</span>';
}
add_shortcode('caption', 'caption_shortcode');
In your blog post
[caption class="headline"]My Text[/caption]
The above would output. If your output is static, this would be the best way to go.
<span class="caption">My Text</span>
If you're sure you want to make it into a plugin, you can do that too. Simply putting this code in a file and putting it into your plugins directory will work. Make sure that you use the proper standards when defining the plugins (see link below).
The last link has both mentioned in the blog post and has a nice plugin that you can use for your own needs. That blog post explains how you can use the shortcodes in widgets as well.
Links
http://www.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/
http://codex.wordpress.org/Shortcode_API
http://codex.wordpress.org/Writing_a_Plugin
http://aaron.jorb.in/blog/2010/02/wordpress-shortcodes-a-how-to-by-example/
Related
I need to to add a short code into a short code in WordPress.
[mycred_link href="http://www.mycred.me"]View portfolio[/mycred_link]
This short code above is a short code from myCred plugin, it gives users points when they click on the link inside the short code.
What I need is:
To show a Facebook share link, inside the short code using. I already have a short code that generates a Facebook share link.
I need something like this:
[mycred_link href="[facebook_share_link]"]View portfolio[/mycred_link]
To give users points when they share my posts on Facebook. I tried it but it didn't work.
I also tried this code below, it Gives point to users and opens facebook.
But facebook says that URL is invalid.
<?php echo do_shortcode( "[mycred_link href='http://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode(get_permalink()); ?>']View portfolio[/mycred_link]"); ?>
If I put double quote after the href the wordpress footer will disappear completely, am running the second code on my footer.php
The problem is inside the code of myCred plugin. As you can see from the source code of the plugin wp-content/plugins/mycred/includes/shortcodes/mycred_link.php what you pass inside the href attribute doesn't get parsed with do_shortcode therefore you will not be able to insert.
As you can see from this answer:Shortcodes inside a shortcode - wordpress wordpress shortcodes doesn't stack automatically.
Luckily for you the function end with:
return apply_filters( 'mycred_link', '<a ' . implode( ' ', $attr ) . '>' . do_shortcode( $link_title ) . '</a>', $atts, $link_title );
So w/o touching the plugin source code you can use the filter and then do something with the href. My guess is something like this (not tested)
Inside your function.php in your theme folder insert something like this:
add_filter('mycred_link','edit_mycred_link',10,3 );
function edit_mycred_link($link_html, $atts, $link_title){
return str_replace('__edit_mycred_link__',do_shortcode('[facebook_share_link]'),$link_html);
}
And then inside your template file / post content
[mycred_link href="__edit_mycred_link__"]View portfolio[/mycred_link]
That should work
I finally solved the problem. I asked the question on wordpress forum and a moderator gave it to me.
<?php
echo do_shortcode( '[mycred_link href="http://www.facebook.com/sharer/sharer.php?u=' . urlencode( get_permalink()) . '"]View portfolio[/mycred_link]');
?>
I have written some PHP and HTML code to insert social share buttons at the top and bottom of each post.
Since the buttons appear twice on the same page, I had to copy paste the same HTML at two locations.
Is there any way for me to save the HTML code just once at some place and then output it using WordPress shortcodes or anything else like that? I just want to avoid copy pasting the same HTML whenever I need to add share buttons in different places.
Update:
The social media links are generated using the following code (similarly for other networks like Twitter etc.):
<i class="fab fa-facebook-f"></i>
I need a way to get the premalink and title etc. properly from the shortcode function.
Thanks.
Well you can achieve this by using hooks. I use the_content hook here. So you don't need to alter any template system. Create a small function in functions.php in your theme.
function yourtheme_before_after($content) {
global $post;
$socialIcons = 'Facebook';
$socialIcons .= '<a>Twitter</a>';
$socialIcons .= '<a>Pintrest</a>';
$beforecontent = $socialIcons;
$aftercontent = $socialIcons;
$fullcontent = $beforecontent . $content . $aftercontent;
return $fullcontent;
}
add_filter('the_content', 'yourtheme_before_after');
Basically, you append the post content after your custom content, then return the result.
Note: You can modify the social icon part using css/html
EDIT :Added the global $post in the function, you can get pretty much everything related to post.
Pretty much every guide ive come across for adding php pages to Wordpress involves adding it at the theme level. e.g. How to add a PHP page to WordPress?. I want to add a new public facing page to multiple sites via a plugin. I dont want to have to add this to dozens of themes. If i can add it ad the plugin level it will work for all sites.
I have this working, but i need some way of injecting this into a theme. In order to get the sidebar and stuff without having to add custom css for each theme.
Ive started by adding a rewrite rule
RewriteRule ^mypage /wp-content/plugins/myplugin/mypage.php [QSA,L]
Then page.php contains
require_once('../../../wp-blog-header.php');
get_header();
//custom php content
//get_sidebar(); // how to get this working.
get_footer();
This also works, but the problem im having is with sidebars. Some themes dont have them and others do. Not all sidebars are 30% etc. I dont know how to build the div structure here to make it work. Some themes are 100% page width, but this looks ugly when viewed on other themes that are a fixed width. I have been able to come up with some compromises, but id rather be able to do this right.
In summery my main question here is. Is it possible to call a method that will inject html into a theme page. e.g. generate_page($html);. This method will then go to page.php of the theme and inject $html into the content area of the theme.
Edit
In an attempt to dynamically inject content into an unknown theme ive done the following
global $post;
$post->post_content = "TEST PAGE content";
$post->post_title = "Page Title";
$post->post_name = "test-page";
include get_template_directory()."/page.php";
This works for some themes and not others. Some themes will display this post just fine, but others (the default wordpres twenty fifteen theme) are displaying this post and then every other post in the database after it. Im not sure where or why its pulling all of these posts, but if i can get it to stop it looks like this will be a valid solution.
Then u could try to load a specific template page in specific case.
function load_my_template( $template )
{
if( is_page() )
$template = plugin_dir_path(__FILE__) . "dir_to/my_template.php";
return $template;
}
Or change the content that is use on loading page
function load_my_content( $content )
{
global $post;
$id = $post->ID;
if( is_page() )
{
ob_start();
include plugin_dir_path(__FILE__) . "dir_to/my_template.php";
$content = ob_get_clean();
}
return $content;
}
In your __construct()
add_filter('template_include', array($this,'load_my_template') );
add_filter("the_content", array($this,"load_my_content" ) );
add_filter("get_the_content", array($this,"load_my_content" ) );
Hope that help u.
Tell me if it's not corresponding with your question.
I'm at an early stage of learning Wordpress (and shortcode), so bear with me:
To me, shortcodes seem like a swiss army knife of not having to use page-specific templates for everything. I like to build as many pages in the wysiwyg as possible, but often I would need some (reusable) php stuff for displaying stuff in a certain way.
Having googled a lot, it seems to me the way to do shortcodes is like:
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
My question is, is it possible to put the html in a separate template-ish file? It seems wrong and verbose to put all this markup here, escape quotes, et.c. Like a template-file for a shortcode, to which the shortcode can pass some Data Transfer Object (or simply just some scoped variables). So: display in template-file, logic for finding data (to pass to said template-file) in shortcode-function (wherever it may be defined, functions.php, separate plugin, or something).
You can set-up views(php files) and then include partial views into those ones. Wordpress allows templates to be includes within other templates to ensure code reuse and its easily modifiable by child themes. You can use this function to include those
get_template_part( $slug );
However, in your case, the short code function needs to return the value to the caller function. So, this setup will not work.
For code that effects FUNCTIONALITY, put your code in a plugin.
For APPEARANCE, put your code in your theme's template files or funtions.php file.
Many beggining WP developers lump all their code into the theme's functions.php file, this is often the wrong place for it (if that code might ever get exported to another theme, for instance). Only put code specific to a specific theme in a theme's functions.php .
To get Wordpress to recognize your plugin, create a php file and start the file like this:
<?php
/*
Plugin Name: My Caption Shortcode Plugin
Description: A really cool plugin
*/
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
?>
Put this file in your plugins directory (usually, you should create a sub directory for each plugin). Plugins are usually held in /wp-content/plugins/ . Then you can activate or deactive the code as a plugin, when you go to the plugins tab in the admin menu.
Of course, this plugin won't do anything as is. Remember that plugin functionality should be hooked into Wordpress via action hooks, filters, and shortcodes. For a shortcode for instance, you'd use the function add_shortcode somewhere to let Wordpress know your function is a shortcode.
I have a wordpress theme running the "static" side of my website (www.url.com/home), and I'm working outside of it for the App (www.url.com/app), but I'm using the wordpress header and footer in the App so as to mantain the look and feel.
The problem is that all pages in the /app directory are coded as a 404 Error. I can somewhat solve this using js to change the title of the page once the page loads, but that isn't a good solution (all search engines ignore those pages).
The wordpress part defines the title like this:
<?php
if ( is_plugin_inactive('wordpress-seo/wp-seo.php') ) {
bloginfo( 'name' );
}
?>
<?php
wp_title("|", true);
?>
I'm not very good with PHP. Can anyone offer a solution so that the title is set normally within the wordpress environment, but instead of automatically tagging the website as an error it will check if the url address starts with "url.com/app" and if it does, it will just title the page something ("Web app", for example).
Thanks, and sorry if the question is lame but I'm really new with PHP.
The correct way is to add a filter into your functions.php file within your theme. So:
wp-content/themes/[YOUR THEME]/functions.php
Add this to the end and then just change the "Your Page Title Here" string. It will add a filter and change any pages that include "/app" in the URI.
function set_title_if_slash_app( $title ){
$full_url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
if (strpos($full_url,"/app") !== false) {
return "Your Page Title Here";
}
return __( 'Home', 'theme_domain' ) . ' | ' . get_bloginfo( 'description' );
}
add_filter( 'wp_title', 'set_title_if_slash_app');