Wordpress: Issues with post_content when using wp_insert_post - wordpress

I have a very weird problem and I would really appreciate your help with it.
I am using wp_insert_post when a user submits a post. It works fine when I submit the form (i.e. the admin of the wordpress site), but when I try the same thing with a different user (i.e. different email, not the admin of the wordpress site), it only shows part of the content I used in the post_content field.
my code:
$my_post = array(
'post_title' => 'My bet',
'post_content' => '<form action="" method="post" id=form_id>' . 'bet ID: ' . '<input readonly id=betid type=text value=' . $bet_id . '>' . '<input id=url name=urlpath type=hidden value=>' . '</form>' ."\r\n match: " . '<span id=match>' . $_POST[event] . '</span>' . "\r\n" . $current_user->user_login . "'s Pick: " . $_POST[bet] . '</span>' . '<span id=friends>' . '</span>' ."\r\n Amount: " . '<span id=amount>' . $_POST[amount] . '</span>' . "<?php include('wp-content/themes/twentyfourteen/test.php') ?>"
'post_status' => 'publish');
When it is executed for the wordpress admin user (user ID=1) it works fine. when it is executed for any other user (user ID!=1) it is not showing the input fields of the form, and more important, not including the test.php I have included.

Related

Add image title and/or description to the caption

Is it possible to manipulate the output of the image captions within the_content in such a way that not only the caption, but also the image title and/or description (defined in the media library) are output under each image?
Possibly via the functions.php?
Yes it is possible to manipulate the on the_content.
The easiest way which it looks like you are wanting to do is add a filter in your functions.php file.
I just pulled this from the Codex and modified a bit so you can get the gist of what I'm talking about.
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
function my_img_caption_shortcode( $output, $attr, $content ) {
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
if ( $attr['id'] ) {
$attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
}
return '<div ' . $attr['id']
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. 'style="max-width: ' . ( 10 + (int) $attr['width'] ) . 'px;">'
. do_shortcode( $content )
. '<p class="wp-caption-text">' . $attr['caption'] . '</p>'
. '</div>';
}
What you can then do is if you see the $attr['caption'], you can modify how you would like it to be. So you can do: get_the_title($attr['id']) to add the title or get_post_meta($attr['id'], '_wp_attachment_image_alt', TRUE); to get the image alt.
To make things easier if you want to grab all the details on a specific attachment you can use a function like this and then get whatever you want.
Here's an example of adding the title to your caption. The format is "Title - Caption"
. '<p class="wp-caption-text">' . get_the_title($attr['id']) . " - " . $attr['caption'] . '</p>'
Link to Code reference: https://developer.wordpress.org/reference/hooks/img_caption_shortcode/
Maybe i'm not understanding your question but it seems to me that you're using the Gutenberg editor?
... All the features you're looking for are already built-in.
If you want to set the alt="" or title="" attributes you can do it on the right side action column, by clicking on the image itself, while in the Gutenberg editor.
For the figcaption, you can set it right under the image, by clicking on the image itself, while in the Gutenberg editor.

Adding a word to numbered list using CSS inline

I am trying to add a word to my numbered list. I want to put the word 'STEP ' before the number. I have seen many answers on how to add it in using CSS and counters (like this: How can I prefix ordered list item numbers with a static string using CSS?), but my list is in a generated email. Is there a way to do it inline?
This is my code:
public function createForgotPasswordEmail($transcriberEmail, $newpassword) {
$attachments = $this->setForgotPasswordAttachmentImages();
$htmlBody = '<h2>Seven steps to reset your password</h2>';
$htmlBody .= '<span style="background-color: yellow;font-weight: bold;">Please follow the 7 steps below carefully to reset your password.</span><br>';
$htmlBody .= '<br>This is a <u>one-time use</u> password. If not used within 72 hours, it will expire and you will need to request another temporary password.<br><br>';
$htmlBody .= '<ol><li><strong>Log out of the template manager</strong> (right click on the template manager in the task bar and choose Exit from the menu).</li><br />';
$htmlBody .= '<img src="cid:exit_from_template_manager.png" width="325" height="149"><br /><br />';
$htmlBody .= '<li>Copy the password below (highlighted in yellow) and use it to sign in to your account on ###.';
$htmlBody .= '<ul><li style="list-style-type: none;padding-top:5px;padding-bottom:5px;">Password: <span style="background-color: yellow;">' . $newpassword . '</span>';
$htmlBody .= '</li></ul></li>';
$htmlBody .= '<br /><img src="cid:log_in_to_###.jpg" width="185" height="175"><br /><br />';
$htmlBody .= '<li>Once you log in, you will be prompted to change your password. ';
$htmlBody .= 'A message will appear in red at the top left of the ### page if your password was changed successfully.</li>';
$htmlBody .= '<br /><img src= "cid:password_successfully_changed_message.png" width="190" height="309"><br /><br />';
$htmlBody .= '<li>You will now be automatically logged out. Please log in again using your new password.</li>';
$htmlBody .= '<li>Update your password manager with your new password, if you use one.</li>';
$htmlBody .= '<li>Log in to the template manager using your new password.</li>';
$htmlBody .= '<br /><img src="cid:login_to_template_manager.png" width="361" height="298"><br /><br />';
$htmlBody .= '<li>If you use a separate password manager for the template manager, update your password there too.</li></ol>';
$htmlBody .= '<br><br>Thank you,<br>###';
$data = [
'From' => '###',
'To' => $transcriberEmail,
'Subject' => 'Your temporary ### password',
'Tag' => 'temporaryPasswordSent',
'HtmlBody' => $htmlBody,
'Attachments' => $attachments
];
$emailClient = new emailClient();
return $emailClient->sendEmailWithInlineAttachments($data);
}
Just to be clear, I want to output:
STEP 1. Log out of the...
STEP 2. Copy the...

After image is deleted WordPress visual editor doesn't remove additional code that I added to default img html output via image_send_to_editor filter

I'm trying to replace the default image html output in WordPress editor with responsive images picture tag.
I created a custom function that replaces the default img html and hooked it into editor via image_send_to_editor filter:
function mystic_responsive_insert_image($html, $id, $caption, $title, $align, $url) {
$image_url = wp_get_attachment_url($id);
$attachment_id = attachment_url_to_postid( $image_url );
$alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
if ( !$alt_text ) { $alt_text = esc_html( get_the_title($post_id) ); }
$thumb_xl_hpt1 = wp_get_attachment_image_src($attachment_id, 'xl-thumb-hpt1');
$thumb_lg_hpt1 = wp_get_attachment_image_src($attachment_id, 'lg-thumb-hpt1');
$thumb_md_hpt1 = wp_get_attachment_image_src($attachment_id, 'md-thumb-hpt1');
$thumb_sm_hpt1 = wp_get_attachment_image_src($attachment_id, 'sm-thumb-hpt1');
$thumb_xs_hpt1 = wp_get_attachment_image_src($attachment_id, 'xs-thumb-hpt1');
$thumb_xl_hpt1_2x = wp_get_attachment_image_src($attachment_id, 'xl-thumb-hpt1-2x');
$thumb_lg_hpt1_2x = wp_get_attachment_image_src($attachment_id, 'lg-thumb-hpt1-2x');
$thumb_md_hpt1_2x = wp_get_attachment_image_src($attachment_id, 'md-thumb-hpt1-2x');
$thumb_sm_hpt1_2x = wp_get_attachment_image_src($attachment_id, 'sm-thumb-hpt1-2x');
$thumb_xs_hpt1_2x = wp_get_attachment_image_src($attachment_id, 'xs-thumb-hpt1-2x');
$thumb_data = array(
'thumb_xl_hpt1_2x' => $thumb_xl_hpt1_2x[0],
'thumb_large_hpt1_2x' => $thumb_lg_hpt1_2x[0],
'thumb_medium_hpt1_2x' => $thumb_md_hpt1_2x[0],
'thumb_small_hpt1_2x' => $thumb_sm_hpt1_2x[0],
'thumb_xs_hpt1_2x' => $thumb_xs_hpt1_2x[0],
'thumb_xl_hpt1' => $thumb_xl_hpt1[0],
'thumb_large_hpt1' => $thumb_lg_hpt1[0],
'thumb_medium_hpt1' => $thumb_md_hpt1[0],
'thumb_small_hpt1' => $thumb_sm_hpt1[0],
'thumb_xs_hpt1' => $thumb_xs_hpt1[0],
'thumb_alt' => $alt_text
);
$html = '<picture>';
$html .= '<!--[if IE 9]><video style="display: none;"><![endif]-->';
$html .= '<source srcset="' . $thumb_data['thumb_xl_hpt1'] . ', ' . $thumb_data['thumb_xl_hpt1_2x'] . ' 2x" media="(min-width: 1200px)">';
$html .= '<source srcset="' . $thumb_data['thumb_large_hpt1'] . ', ' . $thumb_data['thumb_large_hpt1_2x'] . ' 2x" media="(min-width: 992px)">';
$html .= '<source srcset="' . $thumb_data['thumb_medium_hpt1'] . ', ' . $thumb_data['thumb_medium_hpt1_2x'] . ' 2x" media="(min-width: 768px)">';
$html .= '<source srcset="' . $thumb_data['thumb_small_hpt1'] . ', ' . $thumb_data['thumb_small_hpt1_2x'] . ' 2x" media="(min-width: 576px)">';
$html .= '<!--[if IE 9]></video><![endif]-->';
$html .= '<img srcset="' . $thumb_data['thumb_xs_hpt1'] . ', ' . $thumb_data['thumb_xs_hpt1_2x'] . ' 2x" alt="' . $thumb_data['thumb_alt'] . '">';
$html .= '</picture>';
return $html;
}
add_filter( 'image_send_to_editor', 'mystic_responsive_insert_image', 10, 9 );
I then created a test post and inserted the image into it to see if my code works. It does. But then after I deleted the image and inserted another one, inside the chrome inspector I noticed that the additional html code from the deleted image is still present on the page. Everything except the img tag that was placed inside the picture tag is still there. Deleting the image from the editor removed only the img tag.
I'm making this for someone who is not familiar with html and I'm certain that they will not be able to delete the leftover code from the text editor if they ever edit the images in their posts so I need to figure out why is this happening and how can I fix it?
This is because while removing image, visual editor only removes image in img tag and rest of the HTML is still there. On updating, it updates the HTML in database.
Also, It appends previous HTML with new HTML.
You just have to filter data before it is saved. Have to remove 'picture' tag in which there is no image. You can use 'wp_insert_post_data' hook for this.

Woocommerce Dynamic Pricing Plugin: add custom text before price

I'm using the woocommerce Dynamic Pricing Plugin - how can I change the "FROM" field and replace with custom text?
Here's my code which is attempting to filter the original plugins code;
The TEST text works fine, but the price is returning as 0.00
add_filter( 'wc_dynamic_pricing_price_html', 'custom_single_price');
function custom_single_price ($html, $_product) {
$from = '<span class="test">' . _x( 'TEST:', 'min_price', 'woocommerce' ) . ' </span>';
$html = '<del>' . WC_Dynamic_Pricing_Compatibility::wc_price( $base_price ) . '</del><ins> ' . $from . WC_Dynamic_Pricing_Compatibility::wc_price( $display_price ) . '</ins>';
return $html;
}

WordPress: output custom post type not displaying

I'm trying to output a custom post type but nothing shows up. The code is very simple - what could I be missing?
<?php
$favorites = new WP_Query(array('post_type' => 'favorites', 'posts_per_page' => 10));
while ($favorites->have_posts()) : $favorites->the_post();
echo '<li><img class="rounded" src="' . bloginfo('template_directory') . the_post_thumbnail(array(101,99)) . '" alt="' . the_title() . '" /></li>';
endwhile;
?>
Have you checked that;
You have definitely registered a post type called favorites (note plural)
The browser is not choking on your markup (your HTML is invalid - for starters, you should wrap list items in a <ul>
The function simple_fields_get_post_value() exists and is working correctly

Resources