Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter content of pulled posts #1226

Closed
1 task done
IonTulbure opened this issue Jun 12, 2024 · 3 comments
Closed
1 task done

Filter content of pulled posts #1226

IonTulbure opened this issue Jun 12, 2024 · 3 comments
Labels
type:question Further information is requested.

Comments

@IonTulbure
Copy link

Describe your question

Hi. Posts on remote site have shortcodes which insert different html content into post body.
How can we filter post_content of pulled posts (both draft and published post with Distributor).

What action or filter to use for this scenario ? Where it is best to implement this, on the source site or the target one ?

I can strip the html tags with preg_replace on save_post hook but it will run for normal post inserts which isn't alright.

Code of Conduct

  • I agree to follow this project's Code of Conduct
@IonTulbure IonTulbure added the type:question Further information is requested. label Jun 12, 2024
@peterwilsoncc
Copy link
Collaborator

@IonTulbure You can use the dt_pull_post_args filter to modify the content of a post when pulling from one site to another:

<?php

namespace Distributor\Ticket\FilterPulledContent;

/**
 * Modify the content of a post before it is pulled into the local site.
 *
 * @param array $post_args The post arguments passed to `wp_insert_post()` and `wp_update_post()`.
 * @return array The modified post arguments.
 */
function modify_pulled_post_content( $post_args ) {
	// Modify the post content.
	$post_args['post_content'] = 'Modified content';

	return $post_args;
}

add_filter( 'dt_pull_post_args', __NAMESPACE__ . '\\modify_pulled_post_content' );

This code runs on the site that the content is being pulled to, ie the site you visit to access the pull screen.

@IonTulbure
Copy link
Author

IonTulbure commented Jun 18, 2024

@peterwilsoncc Hi. It works, much appreciated !

In case someone is interested. I filtered out the shorcode html with preg_replace. It removes all content inside custom tags like <shortcode> </shortcode> .

/**
 * Filter out shortcode content when posts are pulled.
 * 
 */

add_filter('dt_pull_post_args', 'modify_pulled_post_content');

function modify_pulled_post_content($post_args)
{
    // define a function to remove content inside custom HTML tags
    function remove_custom_tags($content)
    {
        // define the custom tags you want to remove content from
        $tags = array('shortcode');

        foreach ($tags as $tag) {
            // regular expression to match and remove content inside the custom tags
            $pattern = "/<\s*$tag\b[^>]*>.*?<\s*\/\s*$tag\s*>/is";
            $content = preg_replace($pattern, '', $content);
        }

        return $content;
    }

    // modify the post content by removing custom tags
    if (isset($post_args['post_content'])) {
        $post_args['post_content'] = remove_custom_tags($post_args['post_content']);
    }

    return $post_args;
}

@IonTulbure
Copy link
Author

Issue solved

@github-project-automation github-project-automation bot moved this from Incoming to Done in Open Source Practice Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question Further information is requested.
Projects
Archived in project
Development

No branches or pull requests

2 participants