admin管理员组文章数量:1391991
Is there a Wordpress "correct" way to extend WP_Customize_Media_Control to return the filename instead of attachment ID to the wp.customize preview function? I am using the media control to save a font file which I would like to update in the preview on change.
class myUploadControl extends WP_Customize_Media_Control {
public $required = array();
public function returnData($return) {
return 'filename.ttf';
}
}
I'm looking for something where I can tap into like the above and return the filename to wp.customize instead of the ID.
I have noticed that WP_Customize_Image_Control does indeed return the filename instead of the ID, but this is only for images.
Is there a Wordpress "correct" way to extend WP_Customize_Media_Control to return the filename instead of attachment ID to the wp.customize preview function? I am using the media control to save a font file which I would like to update in the preview on change.
class myUploadControl extends WP_Customize_Media_Control {
public $required = array();
public function returnData($return) {
return 'filename.ttf';
}
}
I'm looking for something where I can tap into like the above and return the filename to wp.customize instead of the ID.
I have noticed that WP_Customize_Image_Control does indeed return the filename instead of the ID, but this is only for images.
Share Improve this question edited Feb 27, 2020 at 22:56 Michael Watson asked Feb 27, 2020 at 22:17 Michael WatsonMichael Watson 1033 bronze badges 2- are you trying to save the filename instead of the ID? Or are you trying to display the image? Have you tried using the standard attachment functions to retrieve the URL/filename? – Tom J Nowell ♦ Commented Feb 27, 2020 at 22:37
- Sorry if I didn't explain great. I'm not processing an image - it's a font file. I am happy saving the ID, but would like to pass the filename into the javascript on change of the field so that I can update my CSS in the preview. – Michael Watson Commented Feb 27, 2020 at 22:41
1 Answer
Reset to default 1I have noticed that WP_Customize_Image_Control does indeed return the filename instead of the ID, but this is only for images.
That's because the class extends WP_Customize_Upload_Control
which has the type set to upload
:
class WP_Customize_Upload_Control extends WP_Customize_Media_Control {
public $type = 'upload';
public $mime_type = '';
...
}
And for Customizer controllers using the above class, the JavaScript controller is wp.customize.UploadControl
which saves the file URL instead of the attachment ID.
So if you want to save the URL instead of the ID, then you could either:
Extend
WP_Customize_Upload_Control
:class myUploadControl extends WP_Customize_Upload_Control { ... }
Or when you add the control, pass an instance of
WP_Customize_Upload_Control
:$wp_customize->add_control( new WP_Customize_Upload_Control( $wp_customize, 'your_setting', array( 'label' => 'Label Here', 'section' => 'section_id', ... ) ) );
Note:
$wp_customize
is an instance of theWP_Customize_Manager
class.
But if you have to save the ID, but need to get the URL dynamically in JavaScript, then you could for example use wp.apiRequest()
to make an API request to /wp/v2/media/<media ID>
:
wp.customize( 'your_setting', function( value ) {
value.bind( function( to ) {
wp.apiRequest( { path: '/wp/v2/media/' + to } )
.then( function ( s ) {
console.log( s.source_url ); // logs the full URL to the file
// do your thing
} );
} );
} );
本文标签: phpExtending WPCustomizeMediaControl to return filename
版权声明:本文标题:php - Extending WP_Customize_Media_Control to return filename 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744709372a2621034.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论