admin管理员组文章数量:1279207
I have created a section inside the admin menu, Where it is possible to upload the logo, How can I replace this uploaded image with wordpress logo(header_image())? //admin dashboard page
<img alt="" src="<?php header_image() ?>">
<form action="<?php echo get_stylesheet_directory_uri() ?>/process_upload.php" method="post" enctype="multipart/form-data">
Your Logo: <input type="file" name="profilepicture" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
//upload process code
<?php
require( dirname(__FILE__) . '/../../../wp-load.php' );
$theme_root = get_theme_root();
$wordpress_upload_dir = wp_upload_dir();
$i = 1;
$profilepicture = $_FILES['profilepicture'];
$new_file_path = $wordpress_upload_dir['path'] . '/' . $profilepicture['name'];
$new_file_mime = mime_content_type( $profilepicture['tmp_name'] );
if( empty( $profilepicture ) )
die( 'File is not selected.' );
if( $profilepicture['error'] )
die( $profilepicture['error'] );
if( $profilepicture['size'] > wp_max_upload_size() )
die( 'It is too large than expected.' );
if( !in_array( $new_file_mime, get_allowed_mime_types() ) )
die( 'WordPress doesn\'t allow this type of uploads.' );
while( file_exists( $new_file_path ) ) {
$i++;
$new_file_path = $wordpress_upload_dir['path'] . '/' . $i . '_' . $profilepicture['name'];
}
// looks like everything is OK
if( move_uploaded_file( $profilepicture['tmp_name'], $new_file_path ) ) {
$upload_id = wp_insert_attachment( array(
'guid' => $new_file_path,
'post_mime_type' => $new_file_mime,
'post_title' => preg_replace( '/\.[^.]+$/', '', $profilepicture['name'] ),
'post_content' => '',
'post_status' => 'inherit'
), $new_file_path );
// wp_generate_attachment_metadata() won't work if you do not include this file
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate and save the attachment metas into the database
wp_update_attachment_metadata( $upload_id, wp_generate_attachment_metadata( $upload_id, $new_file_path ) );
// Show the uploaded file 'in browser
wp_redirect( admin_url( '/admin.php?page=coalition-setting' ) );
}
I have created a section inside the admin menu, Where it is possible to upload the logo, How can I replace this uploaded image with wordpress logo(header_image())? //admin dashboard page
<img alt="" src="<?php header_image() ?>">
<form action="<?php echo get_stylesheet_directory_uri() ?>/process_upload.php" method="post" enctype="multipart/form-data">
Your Logo: <input type="file" name="profilepicture" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
//upload process code
<?php
require( dirname(__FILE__) . '/../../../wp-load.php' );
$theme_root = get_theme_root();
$wordpress_upload_dir = wp_upload_dir();
$i = 1;
$profilepicture = $_FILES['profilepicture'];
$new_file_path = $wordpress_upload_dir['path'] . '/' . $profilepicture['name'];
$new_file_mime = mime_content_type( $profilepicture['tmp_name'] );
if( empty( $profilepicture ) )
die( 'File is not selected.' );
if( $profilepicture['error'] )
die( $profilepicture['error'] );
if( $profilepicture['size'] > wp_max_upload_size() )
die( 'It is too large than expected.' );
if( !in_array( $new_file_mime, get_allowed_mime_types() ) )
die( 'WordPress doesn\'t allow this type of uploads.' );
while( file_exists( $new_file_path ) ) {
$i++;
$new_file_path = $wordpress_upload_dir['path'] . '/' . $i . '_' . $profilepicture['name'];
}
// looks like everything is OK
if( move_uploaded_file( $profilepicture['tmp_name'], $new_file_path ) ) {
$upload_id = wp_insert_attachment( array(
'guid' => $new_file_path,
'post_mime_type' => $new_file_mime,
'post_title' => preg_replace( '/\.[^.]+$/', '', $profilepicture['name'] ),
'post_content' => '',
'post_status' => 'inherit'
), $new_file_path );
// wp_generate_attachment_metadata() won't work if you do not include this file
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate and save the attachment metas into the database
wp_update_attachment_metadata( $upload_id, wp_generate_attachment_metadata( $upload_id, $new_file_path ) );
// Show the uploaded file 'in browser
wp_redirect( admin_url( '/admin.php?page=coalition-setting' ) );
}
Share
Improve this question
edited Nov 4, 2021 at 13:35
pooyan golkar
asked Nov 4, 2021 at 12:39
pooyan golkarpooyan golkar
254 bronze badges
2
- And what is your question? – kero Commented Nov 4, 2021 at 12:56
- how can i change header_image() , with my uploaded image – pooyan golkar Commented Nov 4, 2021 at 13:26
1 Answer
Reset to default 1To be honest I don't fully understand why you need this custom code to upload images, it should be possible with the default settings.
To answer your question:
If you dig a bit through the code, you'll see that
header_image()
callsget_header_image()
and returns thatget_header_image()
callsget_theme_mod('header_image', ...)
and will return that (as long as its notremove-header
oris_random_header_image()
returnstrue
).get_theme_mod()
now finally introduces a filter:theme_mod_{$name}
So, the following filter should be able to change the header image to whatever you want:
add_filter('theme_mod_header_image', function ($originalValue) {
// return URL to your desired image
});
本文标签: How Change Wordpress header image from admin menu
版权声明:本文标题:How Change Wordpress header image from admin menu 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741235087a2362852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论