Wordpress action help for post.php?action=edit - wordpress

Question for you regarding wordpress.
I want to display a notification at the top of post.php?action=edit if the get_post_meta is a certain value... any advice how I can more effectively accomplish this? I seem to be getting no response from this code.
add_action('admin_notices', 'HMMultipostMU_notifyChild' );
if( !function_exists( 'HMMultipostMU_notifyChild' ) ){
function HMMultipostMU_notifyChild(){
global $hmMultipostMU;
if( !isset( $hmMultipostMU ) ){
return;
}
if($meta = get_post_meta($post_id, 'HMMultipostMU_parent', true)) {
$parent = unserialize( $meta );
if(!empty($parent)) {
foreach ($parent as $key => $value) {
switch_to_blog( $key );
echo '<div id="message" class="updated highlight"> WARNING: This is a child article! Please <a href='. get_edit_post_link( $value ). '>click here</a> to edit this article as the Parent. Be Aware, this may switch blog sites.<br /> </div>';
}
restore_current_blog();
}
}
}
}

I found my issue! ahhh. $post_id wasn't defined until the page runs... I had to change it to $_GET var. Gr sorry thanks!

Related

WPLMS course purchase condition

I am using WPLMS theme for a course management website. I am using following code to display a button in the details page.
add_filter('wplms_before_course_description','custom_add_second_button');
function custom_add_second_button($course_details){
$custom_link = get_post_meta(get_the_ID(),'Custom_button_link',true);
if(isset($custom_link)){
echo '<a class="full button" href="'.$custom_link.'"><span class="vicon vicon-shopping-cart"></span> Buy This Course</a>';
return $course_details;
}
return $course_details;
}
This is to show a custom add to cart button beside the title of the course. Now what I want is if a student has already purchased this course, this button will not come up. Kindly help me to achieve my goal please. Thanks in advance.
You can use WC wc_customer_bought_product() function to check whether the customer already bought it or not.
add_filter( 'wplms_before_course_description','custom_add_second_button' );
function custom_add_second_button($course_details){
global $product;
$custom_link = get_post_meta( get_the_ID(), 'Custom_button_link', true );
if( is_user_logged_in() ){
$current_user = wp_get_current_user();
if( !wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) && isset( $custom_link ) ){
echo '<a class="full button" href="'.$custom_link.'"><span class="vicon vicon-shopping-cart"></span> Buy This Course</a>';
return $course_details;
}
}else{
if( isset( $custom_link ) ){
echo '<a class="full button" href="'.$custom_link.'"><span class="vicon vicon-shopping-cart"></span> Buy This Course</a>';
return $course_details;
}
}
return $course_details;
}
USEFUL LINKS.
wc_customer_bought_product()
You can use the function bp_course_is_member($course_id,$user_id)) to check if the user is already a member of the course.

Best way to do this? Add tags as images on product pages

I am trying to be clever, but this is a bit beyond my abilities.
I have products with tags, these tags denote if something is environmentally friendly or not. Some of these tags are "biodegradable", compostable" and "recycled" for example.
If a product has these tags, I want to echo it on the front end.
I have the code to do this, and it is working as expected:
$current_tags = get_the_terms( get_the_ID(), 'product_tag' );
//only start if we have some tags
if ( $current_tags && ! is_wp_error( $current_tags ) ) {
//create a list to hold our tags
echo '<ul class="product_tags">';
//for each tag we create a list item
foreach ($current_tags as $tag) {
$tag_title = $tag->name; // tag name
echo '<li><img src="/img/tags/'.$tag_title.'.png"></li>';
}
echo '</ul>';
}
However, the only way to get this working is for me to edit content-single-product.php or single-product.php and place it in my theme in the woocommerce folder.
Is there a better way?
I'd like to control exactly where in the source order of that page it is displayed.
Figured it out. In functions.php:
/**
* Add test
*/
add_action( 'woocommerce_after_single_product_summary', 'tjobbetest', 5 );
function tjobbetest() {
$current_tags = get_the_terms( get_the_ID(), 'product_tag' );
//only start if we have some tags
if ( $current_tags && ! is_wp_error( $current_tags ) ) {
//create a list to hold our tags
echo '<ul class="product_tags">';
//for each tag we create a list item
foreach ($current_tags as $tag) {
$tag_title = $tag->name; // tag name
echo '<li><img src="/img/tags/'.$tag_title.'.png"></li>';
}
echo '</ul>';
}
}

Wordpress unique checkbox

I have created a custom field "headline" in posts that is handled by a checkbox. Now I want that when the post is saved and the checkbox is checked, all "headline" checkboxes are emptied in the other posts. If this works correctly there should only be one other post with that checkbox checked.
function createHeadlineField()
{
$post_id = get_the_ID();
if (get_post_type($post_id) != 'post') {
return;
}
$value = get_post_meta($post_id, '_headline_field', true);
wp_nonce_field('headline_nonce_'.$post_id, 'headline_nonce');
?>
<div class="misc-pub-section misc-pub-section-last">
<label><input type="checkbox" value="1" <?php checked($value, true, true); ?> name="_headline_field" /><?php _e('This post is the top Story', 'pmg'); ?></label>
</div>
<?php
}
function saveHeadlineField($post_id)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (
!isset($_POST['headline_nonce']) ||
!wp_verify_nonce($_POST['headline_nonce'], 'headline_nonce_'.$post_id)
) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
if (isset($_POST['_headline_field'])) {
update_post_meta($post_id, '_headline_field', $_POST['_headline_field']);
} else {
delete_post_meta($post_id, '_headline_field');
}
}
Has anybody a clue how to do that? I guess I have to query the posts for posts with _headline_field values and delete these before updating the actual post.
thx
so if u want to query the posts with the metakey Headline u can do something like that:
$posts = array();
$query = new WP_Query(array('meta_key' => Headline, 'meta_value'=>YOURVALUE, 'posts_per_page' => LIMIT (-1 for endless)))
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$posts[] = $query->post;
}
wp_reset_postdata();
}
return $posts;
I tried to do it like that:
if (isset($_POST['_headline_field'])) {
//query posts with custom field selected
$args = array ( 'meta_key' => '_headline_field', 'meta_value' => '1', LIMIT -1 );
$headline_query = new WP_Query( $args );
if ( have_posts() ):
while ( have_posts() ) :
$headline_query->the_post();
$headline_query->delete_post_meta(get_the_ID(), '_headline_field');
endwhile;
endif;
update_post_meta($post_id, '_headline_field', $_POST['_headline_field']);
Ok, found a solution.
I just used the "delete_post_meta_by_key()" function to erase alle post meta with the key _headline_field before writing the new value into the DB.
if (isset($_POST['_headline_field'])) {
delete_post_meta_by_key( '_headline_field' );
update_post_meta($post_id, '_headline_field', $_POST['_headline_field']);
}

How-to get a menu label via $post-> or $page->ID

Entirely Revised Please Reread
Hello,
The theme I am using displays the page's title as opposed to it's menu label in the breadcrumbs. I am trying to get the breadcrumbs to instead display the associated menu label if it is available and if not then default to the page_title.
I have come up with some code that I think is close. Line 4/// $menu_items = wp_get_nav_menu_items( $slug ); returns null and it should return the nav item that contains $slug of the current post. Obviously, there is something I do not understand.
What I am attempting to do is get the slug of the current post, then using the slug get the nav item post. Then extract the title of the nav item and use that in place of the page title in the breadcrumbs. If the page was not in the nav system then it should default to the page title, as might be the case for a ppc campaign landing page.
if ( is_page() && !$post->post_parent ) {
$title = null;
$slug = mpactMEDIA_get_the_slug( get_the_ID() );
$menu_items = wp_get_nav_menu_items( $slug );
//var_dump((array)$menu_items);
foreach ( (array)$menu_items as $key => $menu_item ) {
$title = $menu_item->post_title;
}
if ( $title ) { echo $delimiter . ' ' . $before . $title . $after; }
else { echo $delimiter . ' ' . $before . get_the_title() . $after; }
}
I'm my functions.php file I have the following function
function mpactMEDIA_get_the_slug( $id=null ){
if( empty($id) ) global $post;
if( empty($post) ) return '';
$id = $post->ID;
endif;
$slug = basename( get_permalink($id) );
return $slug;
}
Thank you in advance,
Tim
I read the question a few times, I got here searching for an answer, ended up making my own.
function get_menu_label_by_post_id($post_id, $menu) {
$menu_title = '';
$nav = wp_get_nav_menu_items($menu);
foreach ( $nav as $item ) {
if ( $post_id == $item->object_id ) {
$menu_title = $item->post_title;
break;
}
}
return ($menu_title !== '') ? $menu_title : get_the_title($post_id);
}
Example usage:
echo get_menu_label_by_post_id($post->ID, 'Primary Nav');
This will return what the menu label is if it finds it, otherwise just the title of the post ID.
Check the documentation for wp_get_nav_menu_items. It doesn't take a page slug as a parameter at all.
If you want to list child pages of a given page, use wp_list_pages and pass a child_of parameter to it.
Also, as a side note, if you know the $post and want the slug, it's just $post->post_name

Wordpress remove shortcode and save for use elsewhere

Trying to remove the gallery shortcode from the post content and save in a variable for use elsewhere in the template. The new Wordpress gallery tool is great for selecting which images they want and assigning captions, hoping to use this to create the gallery, but then pull it out of the content on the front-end.
So this little snipped works just fine for removing the gallery and reapplying formatting... however I want to save that gallery shortcode.
$content = strip_shortcodes( get_the_content() );
$content = apply_filters('the_content', $content);
echo $content;
Hoping to save the shortcode so it can be parsed into an array and used to recreate a custom gallery setup on the front-end. An example of this shortcode I'm trying to save is...
[gallery ids="1079,1073,1074,1075,1078"]
Any suggestions would be greatly appreciated.
Function to grab First Gallery shortcode from post content:
// Return first gallery shortcode
function get_shortcode_gallery ( $post = 0 ) {
if ( $post = get_post($post) ) {
$post_gallery = get_post_gallery($post, false);
if ( ! empty($post_gallery) ) {
$shortcode = "[gallery";
foreach ( $post_gallery as $att => $val ) {
if ( $att !== 'src') {
if ( $att === 'size') $val = "full"; // Set custom attribute value
$shortcode .= " ". $att .'="'. $val .'"'; // Add attribute name and value ( attribute="value")
}
}
$shortcode .= "]";
return $shortcode;
}
}
}
// Example of how to use:
echo do_shortcode( get_shortcode_gallery() );
Function to delete First gallery shortcode from Post content:
// Deletes first gallery shortcode and returns content
function strip_shortcode_gallery( $content ) {
preg_match_all( '/'. get_shortcode_regex() .'/s', $content, $matches, PREG_SET_ORDER );
if ( ! empty( $matches ) ) {
foreach ( $matches as $shortcode ) {
if ( 'gallery' === $shortcode[2] ) {
$pos = strpos( $content, $shortcode[0] );
if ($pos !== false)
return substr_replace( $content, '', $pos, strlen($shortcode[0]) );
}
}
}
return $content;
}
// Example of how to use:
$content = strip_shortcode_gallery( get_the_content() ); // Delete first gallery shortcode from post content
$content = str_replace( ']]>', ']]>', apply_filters( 'the_content', $content ) ); // Apply filter to achieve the same output that the_content() returns
echo $content;
just use the get_shortcode_regex():
<?php
$pattern = get_shortcode_regex();
preg_match_all('/'.$pattern.'/s', $post->post_content, $shortcodes);
?>
that will return an array of all the shortcodes in your content, which you can then output wherever you feel, like so:
<?php
echo do_shortcode($shortcodes[0][1]);
?>
similarly, you could use the array entries to check for shortcodes in your content and remove them with str_replace():
<?php
$content = $post->post_content;
$content = str_replace($shortcodes[0][1],'',$content);
?>
Something like $gallery = do_shortcode('[gallery]'); might work.

Resources