Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter ftroitero

    (@ftroitero)

    That is the right answer.

    Null value at the start => set the value only if its null, inside a useEffect hook with empty dependency array.

    3 years trying and I still can’t figure out how React hooks work ???♀?.

    Obrigado pela ajuda

    Thread Starter ftroitero

    (@ftroitero)

    Your knowledge and pedagogic ability know no boundaries.

    My callback function was not returning the proper values. Once I had a better understanding on how add_filter and wp_insert_post_data work, I could use them to sanitize data.

    function sm_sanitize_title( $data, $postarr, $unsanitized_postarr){
        $data['post_title'] = html_entity_decode($data['post_title']);
    	
    	$quotationMarks = array ('/?/','/?/','/?/','/?/','/','/?/','/?/','/?/');
    	$data['post_title'] = preg_replace($quotationMarks, '"', $data['post_title']);
        return $data;
    }

    add_filter( 'wp_insert_post_data', 'sm_sanitize_title', 1, 3 );

    I could also implement a JS script to validate input on the client side

    function sm_validate_title_client_side() {
    		echo "
    		<script>
    		jQuery('body').on('focus', '[contenteditable]', function() {}).on('paste input', '[contenteditable]', function() {
    			let title = event.target.innerText;
    			let inputIsValid = !title.includes(String.fromCharCode(160));
    			let button = document.querySelector('.editor-post-publish-button__button');
    			if (!inputIsValid) {
    				button.disabled = true; //setting button state to disabled;
    				alert('invalid input');
    			} else {
    				button.disabled = false;
    			}
    		});
    		
    		</script>";
    }
    
    add_action('admin_footer', 'sm_test');
    Thread Starter ftroitero

    (@ftroitero)

    Thanks for the reply.

    I tried using wp_insert_post_data, replacing

    add_action('save_post', 'sm_sanitize_HTML_entities', 99, 3);

    with

    add_filter( 'wp_insert_post_data', 'sm_sanitize_HTML_entities', 99, 3 );

    while keeping my function the same. This caused the post to stop updating. Unfortunately, I have no idea why this might have happened nor how to work around it.

    Then I followed the last option you mentioned in your kind reply and tried to implement a JS solution.

    function sm_test() {
    		echo "
    		<script>
    		jQuery('body').on('click', '.editor-post-publish-button__button', function(event) {
    			let button = this;
    			button.disabled = true; //setting button state to disabled
    			event.preventDefault();
    			alert('Hello! I am an alert box!!');
    			return false;
    		});
    		</script>";
    }
    
    add_action('admin_footer-post.php', 'sm_test');

    Miraculously, I managed to display an alert after clicking on the “Update” button.

    And then I tried to disable the save button. This was in the hope of being able to save the post conditionally according to the post content containing invalid characters or not. This obviously failed and the post is still being saved after clicking.

    Where could I go from here? I believe validating post content on the client side before it’s even is what I’m looking for but I will settle for any other strategy that “works”.

    Is it possible to prevent saving conditionally with a JS script as I was trying to do?

    I also came up with this beautiful article about preventing post publishing on the condition of having a large enough image attached, by using “transitation_post_status”
    https://wpshout.com/stop-post-publishing-php/

    The thing is my condition is not having invalid characters, rather than having an image attached or not.

    • This reply was modified 2 years ago by ftroitero.
Viewing 3 replies - 1 through 3 (of 3 total)