It is necessary that all links added by users to their posts (and the user’s admin panel is disabled, and they cannot draw up links using the built-in editor) are cut off (displayed) either by the specified length of characters or to the domain. The more competently to organize it?
2 answers
If you cut only at output, then you need to use a filter on the_content. Explanation in Russian with examples
If, however, when placed in the database - wp_insert_post / wp_update_post
- The fact that you need to use the the_content filter is logical, but what to do inside this filter? Use some kind of php-function, which cuts off the link only its display? Because if, for example, I cut off the css display, then I don’t know what to do with pictures that turn into links. - Alex Vashchenko
- So you still need to save the link, and cut only the anchor? In any case, this is done with regulars. I would recommend doing this not in the output (the_content), but when writing to the database. if they are not used anywhere else. And so and trimming with css css is quite a normal solution (if we are talking about anchor links, and the pictures are not clear). - SeVlad
- Yes, you only need to trim anchors. It agree, it is better at record in a DB. Just a regular season, I do not know how to write. As for css, the option has already disappeared. I found the function url_shorten, but somehow I need to find all the links in the content and pass each to this function in a loop .. (?) It is these actions (the processing itself) that interests me. - Alex Vashchenko
|
Here is the small code I was looking for (functions.php file):
add_filter( 'the_content', 'content_filter' ); function content_filter( $content ) { $new = preg_replace_callback("/(<[Aa][\s]{1}[^>]*[Hh][Rr][Ee][Ff]=\"[\S]*\"[^>]*>)([^<]+)(<\/a>)/", function($m) { return $m[1] . substr($m[2],0,40) . '..' . $m[3]; }, $content); return $new; } |