WordPress: Rich Snippets in plug-in generated posts - php

I am working on a WordPress plug-in which automatically generates posts from a user´s facebook events (this is specifically for a single website, not trying to publish this).
So the post generating and "posting" really do work fine. I use php-class-html-generator for generating a posts mark-up. It basically looks like this:
$post_desc = HtmlTag::createElement('div');
$post_desc -> addClass("af_post_content");
$post_desc -> setText($event_desc);
...
$post = array(
'post_title' => $name,
'post_content' => $post_desc -> toString(),
'post_status' => 'publish',
'post_category' => $category_id
);
...
wp_insert_post($my_post);
When I look at the post´s mark-up (in the back-end or front-end) everything is alright.
Now to my problem: As these post represent "events" I want to add rich snippet mark-up to the post. Declare them as a event, add start time, location with postal address(https://schema.org/Event). The issue is, if I add the mark-up like this:
$post_desc -> set("itemscope", " ");
$post_desc -> set("itemtype", "http://schema.org/Event");
The "itemscope" and "itemtype" won´t show up in the post´s mark-up.
I tried this outside of WordPress with a single "hello world"-example to test if the php-class-html-generator is the issue, but it correctly generates the attributes.
<?php
require_once ('HtmlTag.class.php');
$post_desc = HtmlTag::createElement('div');
$post_desc -> addClass("af_post_content");
$post_desc -> set("itemscope", " ");
$post_desc -> setText("Hello world!");
echo $post_desc;
?>
Source looks like this:
<div itemscope=" " class="af_post_content">Hello world!</div>
So does WordPress suppress some attributes/tags in a post´s mark-up? I had a similiar problem when generating a <table> tag. If so, can I somehow allow these tags?
There are some plug-ins out there for rich snippets, but these mostly handle mark-up change when manually creating a post.
Thank you!

I wouldn't recommend any schema.org specific plugins, as these only bring noise. At least that was my experience.
Not sure if I can help for the specific problem you have, but you can at least try using Yoast plugin https://yoast.com/wordpress/, which is the SEO plugin that also manages schema.org markup in general.
I know it is not an out-of-the-box solution, but sometimes solutions that sound simple, like this one, may help.

Related

How to change the properties (title, caption) of images in a wordpress page with functions.php?

I have a wordpress website which is in both english and french, meaning that for every english page, there's its french counterpart with the same content. Until now, when I wanted to add something to it like a slideshow, I'd do two of them and add them on their respective pages but I have a problem with galleries and solo images as their caption and title are used directly in those cases. I don't want to upload each image twice with different properties so I wanted to add a function in php that switches the language for me.
My idea was to put tags in the images caption to have something like that :
[EN] EN description
[FR] FR description
And have the function get this string and work it to remove what has to be removed for the current page. My problem is that I don't really know php and even less the WordPress api of which I struggle to understand how it works, what I get when I call a function or even what it's talking about in the functions description.
So my question is this: Is there a way to get all the images of a page and edit their properties ? If yes, how ?
I tried to search for clues on the internet and found this code that I wanted to use as a base but I'm not sure it's working:
$PageID = get_the_ID();
$Post = get_post( $PageID );
$Content = $Post->post_content;
$Regex = '/src="([^"]*)"/';
preg_match_all( $Regex, $Content, $Matches );
foreach( $Matches as $Image ):
echo '<img src="'.$Image.'" alt="'.$Post->post_title.'" title="'.$Post->post_title.'">';
endforeach;
Maybe I'm missing something but when I try to print $Matches or $Image in the console, all I get is "Array" and it gives me a broken image with my page title on its side if I try to run it.
Thank you for your time.
If you want to change HTML markup for each image (ex. add custom attributes to each <img>), maybe this is a good thread: Change html structure of all img tags in Wordpress # wordpress.stackexchange.com
If you want to change how WP outputs the caption for an image, which is inserted based on a shortcode in the editor, then this WP codex page should help: Plugin API/Filter Reference/img caption shortcode # codex.wordpress.org
There is an example in the second link. You should be able to get the image caption in a variable (see $attr['caption']) - and then you should filter it (ex. remove the [EN] description). But you need to figure how to access the language of the page in that function.

will do_shortcode() allow me to run shortcode in content before a line in the template?

G'day folks, I'm attempting to integrate Ajax Availability Calendar into a WordPress site via PHP.
I have multiple calendars set up, and need to display a different one on each property page.
The problem I'm having is that I need to be able to specify the ID e.g. $_GET["id_item"]=2 of the calendar before the <?php require_once 'pathtofile' ?> statement which is in the template.
The result I need to achieve is...
<?php $_GET["id_item"]=2; require_once 'path\to\required.file'; ?>
If I put this code in the template with the appropriate ID, it works, but I need the ID number to change on each page, otherwise I'll always be displaying the calendar with ID number 2.
I currently have custom shortcode (below) with which I can specify the ID in the page content, but I am at a loss as to how to access that ID before the statement in the template.
So what I currently have is...
In Functions.php
function ajaxcalendar_shortcode($atts) {
$args = shortcode_atts(
array(
'id_item' => 1
),
$atts
);
return '<?php $_GET["id_item"]='.$id_item.'; ?>';
}
add_shortcode('cal_display', 'ajaxcalendar_shortcode');
with the shortcode [cal_display id_item = "2"] in the content.
Is it even possible to do this or should I try another approach?
My research suggests that I may be able to use do_shortcode() to achieve this, but I haven't figured out how to use it in this situation, or I may be misinterpreting it's purpose.
I've tried putting this in the page template file:
<?php $content = the_content(); echo do_shortcode($the_content); ?>
<?php require_once 'C:\xampp\htdocs\MooreRiver\availability\ac-includes\cal.inc.php'; ?>
But it continues to display the calendar with ID 1.
Any tips would be appreciated :)

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

Custom post layouts in WordPress

I'm quite new to WordPress, and I'm trying to ease myself into it. I'm currently having a problem with the layout of posts.
One solution to a common layout problem is to require the user to put their content within separate divs within the WYSISYG editor. However, this is obviously not that user friendly.
One solution would be: when they create a new post, there are two separate WYSISWYG editors for that single post. One editor for images, one editor for text. This isn't exclusively a problem of separating images from text — more for separating content from content.
I'm not sure whether this would be with the functions.php or a plugin. I'd rather try and not use plugins if I don't have to.
Any information would be great.
Within the visual editor you can double click an image that you've included and within the advanced tab add your custom classes to it. WP also has alignleft, alignright and more classes for images. If my answer is off I think your question may need to be more pointed.
to solve your first, more specific issue regarding how to separate images from text (content).
As usual, there are many ways to achieve such result. I use this one:
As you may know, you can add images to every single post. The problem you certainly come up against is how to attach specific photos to a specific post without using a gallery placeholder inside the content (WYSISYG editor).
You can do so by uploading photos through "Add media" button. It is essential to "upload" those images to a post not to pick them from the media library. It is my understanding that without plugins this is the only way to genuinely attach images to a post.
The question arises how to get the attachment in your templates.
I use this generic function of mine written in functions.php to fetch attachments of a post:
function get_attachment($args) {
$defaults = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', 'post_parent' => $post->ID);
$options = array_merge($defaults, $args);
return get_posts( $options );
}
Since you can upload other types of media (video, audio), there are examples how to use the mentioned function within your templates:
$gallery_images = get_attachment(array('post_parent' => $post->ID, 'post_mime_type' => 'image'));
$audio_files = get_attachment(array('post_parent' => $post->ID, 'post_mime_type' => 'audio'));
$video_files = get_attachment(array('post_parent' => $post->ID, 'post_mime_type' => 'video'));
To respond to your more generic question – separating content from content - indeed, you can alter the admin interface of "Add post form" by creating custom fields or meta boxes.
The help for creating meta boxes can be found in the Wordpress Codex, for instance here:
http://codex.wordpress.org/Function_Reference/add_meta_box
I hope it enlightens a bit.

How to add a css styled html-list to wordpress, manageable in the dashboard

I would like to add a feature to a WordPress page to display a short list of people, like the one seen here - http://www.blenderbox.com/company/team
I have done this on other sites by styling lists with css. This time however, it's for a client and I can't trust them to copy and paste a <li>, editing the name, job title and img name without messing it up.
What is the best way to go about creating this so that it can be easily reduced/added to in the WordPress dashboard? Basically, so that the user simply clicks "add person" then fills in some fields before updating the page.
I'm new to WordPress (though I understand how it works) but I am competent coding so hopefully an informative nudge in the right direction would be sufficient.
Thanks, Ben.
Use categories. Create a new category called Person or Team or something to that effect. Then use the Post Title for their Name - any other information can be put in the post and then add the image to the Featured Image.
Create a new template for this page by copying over another template file and giving it a header like this to name it.
<?
/*
Template Name: About Us / Team / Whatever
*/
?>
And in the page call only the information you want with something like this:
//Set up the Loop
<?php
global $post;
$args = array (
'category' => 4,
'order' => 'ASC');
$teamposts = get_posts( $args );
foreach( $teamposts as $post ) : setup_postdata($post); ?>
//Call the information you want - put them in <li> if you need
<?php the_title();?>
<?php the_excerpt();?> //OR the_content
<?php the_post_thumbnail('new-image-size');?>
//Close the loop
<?php endforeach; ?>
You can then go on to call anything else you want or run another loop on the same page to call other information.
To get the right sized thumbnail you have called 'new-image-size' - to set this up add a line like this to your functions.php file.
// Post Thumbnails
add_theme_support( 'post-thumbnails' );
// name of the thumbnail, width, height, crop mode
add_image_size( 'front-page-services', 450, 270, true );

Categories