admin管理员组文章数量:1287583
I want to create an image generator that will use a url like : /image-generator?a=1&b=2&c=3
The image generator will be dynamic and create an image based on the params. It will also output the image. So there will be no HTML and the header will return content-type: image/jpg
.
What's the best way to achieve this? I looked into creating a custom route, but apparently you're not supposed to do that. Do I need to create a custom type and a post and set a custom handler for that type? How can I make it go straight to code and skip all the templates?
I want to create an image generator that will use a url like : /image-generator?a=1&b=2&c=3
The image generator will be dynamic and create an image based on the params. It will also output the image. So there will be no HTML and the header will return content-type: image/jpg
.
What's the best way to achieve this? I looked into creating a custom route, but apparently you're not supposed to do that. Do I need to create a custom type and a post and set a custom handler for that type? How can I make it go straight to code and skip all the templates?
Share Improve this question asked Sep 18, 2021 at 14:36 DABDAB 1211 bronze badge1 Answer
Reset to default 3You could use the parse_request
hook. For example:
add_action( 'parse_request', function ( $wp ) {
// Run your actions only if the current request path is exactly 'image-generator'
// as in https://example/image-generator if the site URL is https://example
if ( 'image-generator' === $wp->request ) {
$a = $_GET['a'];
$b = $_GET['b'];
$etc = 'etc';
// ... your code here.
// And then send the HTTP status and also the Content-Type headers.
status_header( 200 );
header( 'Content-Type: image/jpeg' );
// ... echo your image.
exit;
}
} );
And note that status_header()
is a WordPress function for sending HTTP status header. See the documentation for more details.
本文标签: custom post typeshow do i create a specific handler for a url
版权声明:本文标题:custom post types - how do i create a specific handler for a url? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741312347a2371718.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论