{"id":9809,"date":"2013-04-24T18:50:19","date_gmt":"2013-04-24T18:50:19","guid":{"rendered":"http:\/\/musilda.cz\/?p=184"},"modified":"2013-04-24T18:50:19","modified_gmt":"2013-04-24T18:50:19","slug":"prihlasovaci-formular-jako-widget-do-sidebaru","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/prihlasovaci-formular-jako-widget-do-sidebaru\/","title":{"rendered":"P\u0159ihla\u0161ovac\u00ed formul\u00e1\u0159 jako widget do sidebaru"},"content":{"rendered":"

Pokud jste ji\u017e \u0159e\u0161ili, jak umo\u017enit u\u017eivatel\u016fm, jak se p\u0159ihl\u00e1sit p\u0159\u00edmo z hlavn\u00ed strany internetov\u00e9 str\u00e1nky, ur\u010dit\u011b jste pou\u017eili jeden z voln\u011b dostupn\u00fdch plugin\u016f. My si dnes uk\u00e1\u017eeme, jak vytvo\u0159it widget bez nutnosti instalovat plugin. Widget si budeme moci zobrazit v jak\u00e9mkoliv definovan\u00e9m sidebaru.<\/p>\n

Nejprve si ve slo\u017ece \u0161ablony, kterou budeme pou\u017e\u00edvat, vytvo\u0159\u00edme slo\u017eku widgets. N\u00e1\u0161 widget si pojmenujeme Login Form, co\u017e asi nen\u00ed origin\u00e1ln\u00ed, ale pro na\u0161e pot\u0159eby to sta\u010d\u00ed. Do slo\u017eky ulo\u017e\u00edme soubor widget-login-form.php, kter\u00fd je zat\u00edm pr\u00e1zd\u00fd. Nyn\u00ed ho za\u010dneme zapl\u0148ovat.<\/p>\n

Na za\u010d\u00e1tek souboru vlo\u017e\u00edme \u00fadaje, s popisem a n\u00e1zvem widgetu. Tato \u010d\u00e1st je d\u016fle\u017eit\u00e1, podle n\u00ed wordpress definuje widget:<\/p>\n

\/*-----------------------------------------------------------------------------------\n\n\tPlugin Name: Sidebar login form\n\tPlugin URI:\n\tDescription: A widget with login form\n\tVersion: 1.0\n\tAuthor:\n\tAuthor URI:\n\n-----------------------------------------------------------------------------------*\/<\/pre>\n

N\u00e1sledn\u011b widget zaregistrujeme:<\/p>\n

\/\/ Register widget\nfunction vm_login_form_widgets() {\n\tregister_widget( 'vm_Login_Form_Widget' );\n}<\/pre>\n

P\u0159id\u00e1me incializaci widgetu:<\/p>\n

\/\/ Add function to widgets_init that'll load our widget\nadd_action( 'widgets_init', 'vm_login_form_widgets' );<\/pre>\n

V\u0161echny widgety vych\u00e1z\u00ed z t\u0159\u00eddy WP_Widget, kterou pomoc\u00ed nov\u00e9 t\u0159\u00eddy roz\u0161\u00ed\u0159\u00edme:<\/p>\n

\/\/ Widget class\nclass vm_login_form_widget extends WP_Widget {\n}<\/pre>\n

P\u0159\u00edpravu m\u00e1me hotovou a nyn\u00ed si do t\u0159\u00eddy vytvo\u0159\u00edme funkce, kter\u00e9 definuj\u00ed vlastn\u00ed widget.
\nVytvo\u0159\u00edme funkci se stejn\u00fdm n\u00e1zvem, kter\u00fd jsme pou\u017eili p\u0159i registraci widgetu a kter\u00e1 definuje z\u00e1kladn\u00ed vlastnosti widetu:<\/p>\n

function vm_Login_Form_Widget() {\n\n\t\/\/ Widget settings\n\t$widget_ops = array(\n\t\t'classname' => 'vm_login_form_widget',\n\t\t'description' => __('A widget display login form in sidebar.', 'theme')\n\t);\n\n\t\/\/ Widget control settings\n\t$control_ops = array(\n\t\t'width' => 259,\n\t\t'height' => 200,\n\t\t'id_base' => 'vm_login_form_widget'\n\t);\n\n\t\/\/ Create the widget\n\t$this->WP_Widget( 'vm_login_form_widget', __('Login Form', 'theme'),\n        $widget_ops, $control_ops );\n\n}<\/pre>\n

Ur\u010dit\u011b jste si v\u0161imli, \u017ee v\u0161echny texty, kter\u00e9 se budou zobrazovat, vkl\u00e1d\u00e1me pomoc\u00ed __(), co\u017e je funkce p\u0159ekladu, d\u00edky tomu umo\u017en\u00edte p\u0159\u00edpadn\u00fd p\u0159eklad \u0161ablony. Jak p\u0159ipravit \u0161ablonu pro p\u0159eklad bude t\u00e9ma jednoho z dal\u0161\u00edch \u010dl\u00e1nk\u016f, ale ji\u017e nyn\u00ed se budeme sna\u017eit d\u011blat widget p\u0159ipraven\u00fd na p\u0159eklad.
\nDal\u0161\u00ed funkc\u00ed bude samotn\u00fd widget:<\/p>\n

function widget( $args, $instance ) {\n\textract( $args );\n\n\t\/\/ Our variables from the widget settings\n\t$title = apply_filters('widget_title', $instance['title'] );\n\n\t\/\/ Before widget (defined by theme functions file)\n\techo $before_widget;\n\n\t\/\/ Display the widget title if one was input\n\tif ( $title )\n\techo $before_title . $title . $after_title;\n\n  if ( is_user_logged_in() ) { global $current_user; get_currentuserinfo();\n  echo '<\/pre>\n

‚.__(‚Login as:‘,’theme‘).‘ ‚.$current_user->user_login.‘
\n‚.__(‚Logout‘,’theme‘).‘<\/a><\/p>\n

';\n\n  }else{\n\n  echo'<\/pre>\n

\n

';\n\t}\n\n\t\/\/ After widget (defined by theme functions file)\n\techo $after_widget;\n}<\/pre>\n

Nejprve zjist\u00edme, zda je u\u017eivatel p\u0159ihl\u00e1\u0161en, pokud ano, zobraz\u00edme jeho nick a link na odhl\u00e1\u0161en\u00ed. Pokud nen\u00ed p\u0159ihl\u00e1\u0161en, zobraz\u00edme formul\u00e1\u0159.
\nP\u0159edposledn\u00ed funkc\u00ed, kterou budeme pot\u0159ebovat, je update, kter\u00e1 ukl\u00e1d\u00e1 zm\u011bny ve widgetu. My zde m\u00e1me jen title, proto\u017ee nic jin\u00e9ho nepot\u0159ebujeme:<\/p>\n

function update( $new_instance, $old_instance ) {\n\t$instance = $old_instance;\n\n\t\/\/ Strip tags to remove HTML (important for text inputs)\n\t$instance['title'] = strip_tags( $new_instance['title'] );\n\n\treturn $instance;\n}<\/pre>\n

Posledn\u00ed je samotn\u00fd formul\u00e1\u0159, kter\u00fd se bude zobrazovat po p\u0159id\u00e1n\u00ed widgetu do sidebaru v administraci:\u00a8<\/p>\n

function form( $instance ) {\n\n\t\/\/ Set up some default widget settings\n\t$defaults = array(\n\t\t'title' => '',\n\t);\n\n\t$instance = wp_parse_args( (array) $instance, $defaults ); ?><\/pre>\n

\t\n\"\"<\/a><\/pre>\n

Po p\u0159eta\u017een\u00ed do sidebaru se widget rozbal\u00ed a m\u016f\u017eeme upravit titulek:
\n
\"\"<\/a><\/p>\n

Po ulo\u017een\u00ed se n\u00e1m p\u0159ihla\u0161ovac\u00ed formul\u00e1\u0159 kr\u00e1sn\u011b zobraz\u00ed v sidebaru:
\n
\"\"<\/a><\/p>\n

Widget v akci si m\u016f\u017eete prohl\u00e9dnout zde<\/a>
\nV n\u011bkter\u00e9m z dal\u0161\u00edch \u010dl\u00e1nk\u016f si uk\u00e1\u017eeme, jak omezit p\u0159ihl\u00e1\u0161en\u00e9mu u\u017eivateli p\u0159\u00edstup do administrace.<\/p>\n","protected":false},"excerpt":{"rendered":"

Pokud jste ji\u017e \u0159e\u0161ili, jak umo\u017enit u\u017eivatel\u016fm, jak se p\u0159ihl\u00e1sit p\u0159\u00edmo z hlavn\u00ed strany internetov\u00e9 str\u00e1nky, ur\u010dit\u011b jste pou\u017eili jeden z voln\u011b dostupn\u00fdch plugin\u016f. My si dnes uk\u00e1\u017eeme, jak vytvo\u0159it widget bez nutnosti instalovat plugin. Widget si budeme moci zobrazit v jak\u00e9mkoliv definovan\u00e9m sidebaru. Nejprve si ve slo\u017ece \u0161ablony, kterou budeme pou\u017e\u00edvat, vytvo\u0159\u00edme slo\u017eku widgets.<\/p>\n","protected":false},"author":1,"featured_media":8624,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[12,30],"tags":[413,415,502,528],"class_list":["post-9809","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-navody","category-wordpress","tag-widget","tag-widget-navod","tag-wordpress-navod","tag-wordpress-tutorial"],"_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/9809"}],"collection":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/comments?post=9809"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/9809\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/8624"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=9809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=9809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=9809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}