Mučim se sa ovim ( za nekoga jednostavnim zadatkom :) ) skoro dva dana.....
Cilj mi je da napravim plugin koji će moći da uploaduje sliku u admin panel a kasnije i na front end...
Polje za unos teksta radi i tu tekst koji upišem kad kliknem na save button ostaje upisan, ali problem je sa uploadovanjem image (logo) koje neće da se sačuva ni u bazi a kamoli da se pojavi na admin panelu....
Evo i koda:
Code:
class DM_options{
public $options;
public function __construct(){
$this->options=get_option('mynewplugin_settings_group');
}
public function mynewplugin_settings_menu() {
add_options_page(
/*1*/ 'My Plugin Settings',
/*2*/ 'My Plugin',
/*3*/ 'manage_options',
/*4*/ 'mynewplugin_settings',
/*5*/ array($this,'mynewplugin_settings_page')
);
}
/** Settings Page Content **/
public function mynewplugin_settings_page() {
?>
<div class="wrap">
<h2>My Plugin</h2>
<p>Some text describing what the plugin settings do.</p>
<form method="post" action="options.php" enctype="multipart/form-data">
<?php
// Output the settings sections.
do_settings_sections( 'mynewplugin_settings' );
// Output the hidden fields, nonce, etc.
settings_fields( 'mynewplugin_settings_group' );
// Submit button.
submit_button();
?>
</form>
</div>
<?php
}
/** Settings Initialization **/
public function mynewplugin_settings_init() {
/** Setting section 1. **/
add_settings_section(
/*1*/ 'mynewplugin_settings_section_1',
/*2*/ 'Section 1',
/*3*/ array($this,'mynewplugin_settings_section_1_callback'),
/*4*/ 'mynewplugin_settings'
);
// Field 1.
add_settings_field(
/*1*/ 'mynewplugin_field_1',
/*2*/ 'Field 1',
/*3*/ array($this,'mynewplugin_field_1_input'),
/*4*/ 'mynewplugin_settings',
/*5*/ 'mynewplugin_settings_section_1'
);
// Register this field with our settings group.
register_setting( 'mynewplugin_settings_group', 'mynewplugin_field_1' );
/** Section 2 **/
add_settings_section(
/*1*/ 'mynewplugin_settings_section_logo',
/*2*/ 'Section logo',
/*3*/ array($this,'mynewplugin_settings_section_logo_callback'),
/*4*/ 'mynewplugin_settings'
);
// Field 2.
add_settings_field(
/*1*/ 'mynewplugin_field_logo',
/*2*/ 'Field logo',
/*3*/ array($this,'mynewplugin_field_logo_input'),
/*4*/ 'mynewplugin_settings',
/*5*/ 'mynewplugin_settings_section_logo'
);
//Register this field with our settings group and calback function
register_setting( 'mynewplugin_settings_group', 'mynewplugin_field_logo',array($this,"dm_validate_settings"));
}
public function mynewplugin_settings_section_1_callback() {
echo( 'Some info about this section.' );
}
public function mynewplugin_settings_section_logo_callback() {
echo( 'We should see a picture nearly .' );
}
public function dm_validate_settings ($plugin_options){
if(!empty($FILES['mynewplugin_field_logo_upload']["tmp_name"])){
$override= array("test_form" => false);
$file= wp_handle_upload($FILES["mynewplugin_field_logo_upload"],$override);
$plugin_options["mynewplugin_field_logo"]=$file["url"];
}else{
$plugin_options["mynewplugin_field_logo"]= $this->options["mynewplugin_field_logo"];
}
return $plugin_options;
}
/** Field 1 Input **/
function mynewplugin_field_1_input() {
echo( '<input type="text" name="mynewplugin_field_1" id="mynewplugin_field_1" value="'. get_option( 'mynewplugin_field_1' ) .'" />' );
}
/** Field 2 Input **/
public function mynewplugin_field_logo_input() {
echo( '<input type="file" name="mynewplugin_field_logo_upload">');
if(isset($this->options['mynewplugin_field_logo'] )) {
echo "<img src='{$this->options['mynewplugin_field_logo']}'>";
}
}
}
add_action( 'admin_menu',function(){
$lets = new DM_options();
$lets->mynewplugin_settings_menu();
});
add_action( 'admin_init',function(){
$lets2 = new DM_options();
$lets2->mynewplugin_settings_init();
});
class DM_options{
public $options;
public function __construct(){
$this->options=get_option('mynewplugin_settings_group');
}
public function mynewplugin_settings_menu() {
add_options_page(
/*1*/ 'My Plugin Settings',
/*2*/ 'My Plugin',
/*3*/ 'manage_options',
/*4*/ 'mynewplugin_settings',
/*5*/ array($this,'mynewplugin_settings_page')
);
}
/** Settings Page Content **/
public function mynewplugin_settings_page() {
?>
<div class="wrap">
<h2>My Plugin</h2>
<p>Some text describing what the plugin settings do.</p>
<form method="post" action="options.php" enctype="multipart/form-data">
<?php
// Output the settings sections.
do_settings_sections( 'mynewplugin_settings' );
// Output the hidden fields, nonce, etc.
settings_fields( 'mynewplugin_settings_group' );
// Submit button.
submit_button();
?>
</form>
</div>
<?php
}
/** Settings Initialization **/
public function mynewplugin_settings_init() {
/** Setting section 1. **/
add_settings_section(
/*1*/ 'mynewplugin_settings_section_1',
/*2*/ 'Section 1',
/*3*/ array($this,'mynewplugin_settings_section_1_callback'),
/*4*/ 'mynewplugin_settings'
);
// Field 1.
add_settings_field(
/*1*/ 'mynewplugin_field_1',
/*2*/ 'Field 1',
/*3*/ array($this,'mynewplugin_field_1_input'),
/*4*/ 'mynewplugin_settings',
/*5*/ 'mynewplugin_settings_section_1'
);
// Register this field with our settings group.
register_setting( 'mynewplugin_settings_group', 'mynewplugin_field_1' );
/** Section 2 **/
add_settings_section(
/*1*/ 'mynewplugin_settings_section_logo',
/*2*/ 'Section logo',
/*3*/ array($this,'mynewplugin_settings_section_logo_callback'),
/*4*/ 'mynewplugin_settings'
);
// Field 2.
add_settings_field(
/*1*/ 'mynewplugin_field_logo',
/*2*/ 'Field logo',
/*3*/ array($this,'mynewplugin_field_logo_input'),
/*4*/ 'mynewplugin_settings',
/*5*/ 'mynewplugin_settings_section_logo'
);
//Register this field with our settings group and calback function
register_setting( 'mynewplugin_settings_group', 'mynewplugin_field_logo',array($this,"dm_validate_settings"));
}
public function mynewplugin_settings_section_1_callback() {
echo( 'Some info about this section.' );
}
public function mynewplugin_settings_section_logo_callback() {
echo( 'We should see a picture nearly .' );
}
public function dm_validate_settings ($plugin_options){
if(!empty($FILES['mynewplugin_field_logo_upload']["tmp_name"])){
$override= array("test_form" => false);
$file= wp_handle_upload($FILES["mynewplugin_field_logo_upload"],$override);
$plugin_options["mynewplugin_field_logo"]=$file["url"];
}else{
$plugin_options["mynewplugin_field_logo"]= $this->options["mynewplugin_field_logo"];
}
return $plugin_options;
}
/** Field 1 Input **/
function mynewplugin_field_1_input() {
echo( '<input type="text" name="mynewplugin_field_1" id="mynewplugin_field_1" value="'. get_option( 'mynewplugin_field_1' ) .'" />' );
}
/** Field 2 Input **/
public function mynewplugin_field_logo_input() {
echo( '<input type="file" name="mynewplugin_field_logo_upload">');
if(isset($this->options['mynewplugin_field_logo'] )) {
echo "<img src='{$this->options['mynewplugin_field_logo']}'>";
}
}
}
add_action( 'admin_menu',function(){
$lets = new DM_options();
$lets->mynewplugin_settings_menu();
});
add_action( 'admin_init',function(){
$lets2 = new DM_options();
$lets2->mynewplugin_settings_init();
});
Hvla unaprijed na pomoći...:)