ﯽﻧﺎﺳﺭ ﺯﻭﺭ ﻪﺑ ﻭ ﺩﺎﺠﯾﺍ - PHP ﺎﺑ ﺱﺮﭘﺩﺭﻭ ﯼﺎﻫ ﺖﺴﭘ ﺖﯾﺮﯾﺪﻣ
- <span>1. ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﺱﺮﭘﺩﺭﻭ ﺕﺎﺤﻔﺻ ﻭ ﺎﻫ ﺖﺴﭘ ﺩﺎﺠﯾﺍ PHP – 101 ﺵﺯﻮﻣﺁ
- 2. ﻥﺪﻧﺍﻮﺧ ﻝﺎﺣ ﺭﺩ: ﺎﺑ ﺱﺮﭘﺩﺭﻭ ﯼﺎﻫ ﺖﺴﭘ ﺖﯾﺮﯾﺪﻣ PHP – ﯽﻧﺎﺳﺭ
.ﻢﯿﻨﮐ ﺩﺎﺠﯾﺍ ﺩﺎﻤﺘﻋﺍ ﻞﺑﺎﻗ ﺖﯾﺎﻬﻧ ﺭﺩ ﻭ ﯼﻮﻗ PHP Wrapper ﺱﻼﮐ ﮏﯾ ﺎﺗ ﻢﯿﻫﺩ ﺵﺮﺘﺴﮔ ﻡﺩﺍﺩ ﺭ
.(!ﺩﻮﺑ ﯽﻧﻻﻮﻃ ﯽﻤﮐ ﻪﻟﺎﻘﻣ ﺩﻮﺧ ﺭﺩ ﻢﯿﻘﺘﺴﻣ ﻥﺪﻧﺎﺒﺴﭼ ﯼﺍﺮﺑ) ﺪﯿﻨﮐ ﺍﺪﯿﭘ ﻦﻣ Github ﻥﺯﺎﺨﻣ ﺯ
ﻢﯿﺳﺮﺑ ﻥﺁ ﻪﺑ ﻢﯿﻫﺍﻮﺧ ﯽﻣ ﻪﭽﻧﺁ:
- .PHP ﺪﯿﻟﻮﺗ ﻪﺟﺭﺩ ﺭﺩ ﺩﺎﻤﺘﻋﺍ ﻞﺑﺎﻗ ﻭ ﯼﺩﺮﺑﺭﺎﮐ ﻼًﻣﺎﮐ ﺏﻮﭼﺭﺎﭼ ﮏﯾ
- .ﺪﯿﺷﺎﺑ ﺩﻮﺟﻮﻣ ﻞﺒﻗ ﺯﺍ ﯼﺎﻫ ﺖﺴﭘ ﯽﻧﺎﺳﺭ ﺯﻭﺭ ﻪﺑ ﻭ ﺖﯾﺮﯾﺪﻣ ﻦﯿﻨﭽﻤﻫ ﻪﮑﻠﺑ ،ﺖﺴﭘ ﺩﺎﺠﯾﺍ ﺎﻬﻨﺗ
- .ﮓﻠﺳﺍ ﺎﯾ ﻥﺍﻮﻨﻋ ؛ﻪﺳﺎﻨﺷ .ﺪﯿﺷﺎﺑ ﺎﻬﻧﺁ ﻂﺳﻮﺗ ﺎﻫ ﺖﺴﭘ ﯼﺭﺎﮑﺘﺳﺩ ﻭ ﻮﺠﺘﺴﺟ ﻪﺑ ﺭﺩﺎﻗ
ﻡﺎﮔ ﻪﺑ ﻡﺎﮔ ﯼﺎﻤﻨﻫﺍﺭ ﻭ ﻩﺩﺎﻔﺘﺳﺍ ﺕﺎﮑﻧ :ﺩﻮﺑ ﺪﻫﺍﻮﺧ ﺶﺨﺑ 2 ﯼﺍﺭﺍﺩ ﻪﻟﺎﻘﻣ ﻦﯾﺍ. !ﻢﯿﻨﮐ ﻉﻭﺮﺷ ﺪﯿﯾﺎﯿﺑ ،ﯼﺍ ﻪﻣﺪﻘﻣ ﭻﯿﻫ ﻥﻭﺪﺑ .ﺖﺷﺍﺩ ﻢﯿﻫﺍﻮﺧ ﻞﯾﺎﻓ ﻦﯾﺍ ﺭﺩ ﺩﻮﺟﻮﻣ ﯼﺎﻫﺪﮐ ﻪﺑ ﯽﻫﺎﮕﻧ ﺵﺯﻮﻣﺁ ﻦﯾﺍ ﻡﻭﺩ ﻪﻤﯿﻧ ﺭﺩ !ﺩﺮﮐ ﺪﻫﺍﻮﺨ .ﺪﯿﻨﮐ ﯼﺯﺎﺳ ﻪﻧﻮﻤﻧ ﺮﯾﺯ ﺕﺭﻮﺻ ﻪﺑ ﺍﺭ ﺱﻼﮐ Post Controller ؟ﺪﺳﺭ ﯽﻣ ﺮﻈﻧ ﻪﺑ ﻩﺩﺎﺳ ﯽﻓﺎﮐ ﻩﺯﺍﺪﻧﺍ ﻪﺑ .ﺪﯿﺷﺎﺑ ﻩﺩﺮﮐ ﺍﺪﯿﭘ ﯽﻧﺎﺳﺭ ﺯﻭﺭ ﻥﺍﻮﻨﻋ ﭗﯾﺎﺗ ﺍﻮﺘﺤﻣ ﻩﺪﻨﺴﯾﻮﻧ Slug/"Name" (ﺕﺎﺤﻔﺻ ﻂﻘﻓ) ﻮﮕﻟﺍ ﺖﻟﺎﯾﺍ ؛ﺪﯿﻫﺩ ﻡﺎﺠﻧﺍ ﺍﺭ ﺭﺎﮐ ﻦﯾﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺮﯾﺯ ﺪﮐ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ .ﺖﺳﺍ ﻥﺍﻮﻨﻋ ﻪﻋﻮﻤﺠﻣ ﻦﺘﺷﺍﺩ .ﺪﯿﻨﮐ ﺍﺮﺟﺍ ﺍﺭ ﺩﺎﺠﯾﺍ ﺪﺘﻣ ﯽﮔﺩﺎﺳ ﻪﺑ ،ﻢﯿﻈﻨﺗ ﺯﺍ ﺲﭘ ﻪﺟﻮﺗ: ﯽﻣ ﯽﺳﺭﺮﺑ ﺵﻭﺭ ﻦﯾﺍﺩﺭﺍﺩ ﺩﻮﺟﻭ ﻡﺎﻧ ﻦﯿﻤﻫ ﺎﺑ ﯼﺮﮕﯾﺩ ﺖﺴﭘ ﺎﯾ .ﻢﯿﻨﮐ ﺍﺪﯿﭘ ﻮﺠﺘﺴﺟ ﻊﺑﺎﺗ ﺎﺑ ﺍﺭ ﻥﺁ ﺪﯾﺎﺑ ﺍﺪﺘﺑﺍ ،ﻢﯿﻨﮐ ﺯﻭﺭ ﻪﺑ ﺍﺭ ﺩﻮﺟﻮﻣ ﺖﺴﭘ ﮏﯾ ﻢﯿﻧﺍﻮﺘﺑ .ﻢﯿﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﻮﺠﺘﺴﺟ ﺵﻭﺭ ﺯﺍ ﺭﺎﮐ ﻦﯾﺍ ﯼﺍﺮﺑ .ﻮﺠﺘﺴﺟ ﯼﺍﺮﺑ ﯼﺍ ﻩﺩﺍﺩ ﻭ ﻮﺠﺘﺴﺟ ﯼﺍﺮﺑ ﯽﮔﮋﯾﻭ ،ﺩﺮﯾﺬﭘ ﯽﻣ ﺍﺭ ﺮﺘﻣﺍﺭﺎﭘ 2 ﺵﻭﺭ ﻦﯾﺍ .ﺪﯿﻨﮐ ﻮﺠﺘﺴﺟ ﻥﺍﻮﻨﻋ ﺱﺎﺳﺍ ﺮﺑ ﺪﯿﻧﺍﻮﺗ ﯽﻣ .ﺪﯿﻨﮐ ﻮﺠﺘﺴﺟ ID ﺎﺑ ﺪﯿﻧﺍﻮﺗ ﯽﻣ .ﺪﯿﻨﮐ ﻮﺠﺘﺴﺟ slug ﺎﺑ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﻭ .ﺪﯿﻫﺩ ﺶﯾﺎﻤﻧ ﻭ ﺪﯾﺮﯿﮕﺑ ﺱﺎﻤﺗ ﺮﯾﺯ ﺪﮐ ﺎﺑ ﺪﯿﻧﺍﻮﺗ ﯽﻣ .ﺩﻮﺷ ﯽﻣ ﻪﻓﺎﺿﺍ $errors .ﺪﯿﻨﮐ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺍﺭ ﯽﻧﺎﺳﺭ ﺯﻭﺭ ﻪﺑ ﺵﻭﺭ ﯽﮔﺩﺎﺳ ﻪﺑ ،ﺎﻫ ﯽﮔﮋﯾﻭ ﻢﯿﻈﻨﺗ ﺯﺍ ﺲﭘ .ﺪﯿﻫﺩ ﺹﺎﺼﺘﺧﺍ .ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺮﯾﺯ ﺪﮐ ﺯﺍ ،(ID=1) ﺪﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ 1 ﺖﺴﭘ ﻥﺍﻮﻨﻋ ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﺮﮔﺍ ،ﻝﺎﺜ .ﺩﺮﮐ ﺯﻭﺭ ﻪﺑ ﻖﯾﺮﻃ ﻦﯾﺍ ﺯﺍ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﺎﻫ ﯽﮔﮋﯾﻭ ﻡﺎﻤﺗ .ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ get_vars ﺪﺘﻣ ﺯﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ ﯽﺑﺎﯾﺯﺎﺑ ﯼﺍﺮﺑ .ﺪﯿﯾﻮﮕﺑ ﻦﻣ ﻪﺑ Github ﺭﺩ ﻥﺁ ﺩﺭﻮﻣ ﺭﺩ ﺎًﻔﻄﻟ ،ﺪﯾﺩﺮﮐ ﺍﺪﯿﭘ ﯽﻟﺎﮑﺷﺍ ﺮﮔﺍ .ﺪﯿﺳﺮﭙﺑ ﻦﻣ ﺯﺍ ﺎ ؟ﺪﯿﺘﺴﻫ ﻩﺩﺎﻣﺁ ﻡﻭﺩ ﺖﻤﺴﻗ ﯼﺍﺮﺑ .ﻢﯾﺩﺮﮔﺮﺑ ﺍﺪﺘﺑﺍ ﻪﺑ ﺪﯿﻫﺩ ﻩﺯﺎﺟﺍ ،ﻢﯾﺪﯾﺩ ﺍﺭ ﺱﻼﮐ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻩﻮﺤﻧ ﻥﻮﻨﮐﺍ .ﻢﯿﻨﮐ ﻡﻼﻋﺍ ﺍﺭ ﺩﺮﮐ ﻢﯿﻫﺍﻮﺧ ﻩﺩﺎﻔﺘﺳﺍ ﻪﮐ ﺍﺭ ﯽﯾﺎﻫﺮﯿﻐﺘﻣ ﻪﻤﻫ ﻪﮐ ﺖﺳﺍ ﻦﯾﺍ ﺎﻣ ﯼﺎﻫﺭﺎﮐ ﺖﺴﯿﻟ .ﻢﯿﻨﮐ ﻩﺮﯿﺧﺫ ﺍﺭ ﻥﺁ ﻢﯿﻧﺍﻮﺘﺑ ﻢﯿﻫﺍﻮﺧ ﯽﻣ ﻪﮐ ﺖﺳﺍ ﯽﯾﺎﻫ ﻩﺩﺍﺩ ﺯﺍ ﯽﺘﺴﯿﻟ ﺎﺠﻨﯾﺍ ﺭﺩ ،ﺪﯿﻧﺰﺑ .ﺪﻧﺭﺍﺩﯽﻣ ﻪﮕﻧ ﺍﺭ ﺩﻮﺟﻮﻣ ﺖﺴﭘ ﻪﺑ ﻁﻮﺑﺮﻣ ﯼﺎﻫﻩﺩﺍﺩ ﻥﺁ Permalink ﻭ Post ID ﯼﺎ .ﺪﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺰﯿﻧ ﺍﺭ()get_var ﻊﺑﺎﺗ ﺪﻧﻮﺸﯿﭘ ﻪﮐ ﺪﯿﺷﺎﺑ ﻪﺘﺷﺍﺩ ﺩﺎﯾ ﻪﺑ ﺪﯾﺩﺍﺩ ﺮﯿﯿﻐﺗ ﺍﺭ .ﻢﯾﺯﺍﺩﺮﭙﺑ ﺩﻮﺧ ﺩﺮﮑﻠﻤﻋ ﻦﯿﻟﻭﺍ ﻪﺑ ﺪﯿﯾﺎﯿﺑ ،ﻢﯾﺩﺮﮐ ﻢﯿﻈﻨﺗ ﺍﺭ ﺰﯿﭼ ﻪﻤﻫ ﻪﮐ ﻥﻮﻨﮐﺍ ،ﺖﺳﺭﺩ .ﺩﻮﺷ ﯽﻣ ﺍﺮﺟﺍ ﻪﻠﺻﺎﻓﻼﺑ ،ﺱﺮﭘﺩﺭﻭ ﺮﺳ ﻪﺑ ﻊﺑﺎﺗ ﻥﺩﺮﮐ ﻪﻓﺎﺿﺍ ﯼﺎﺟ ﻪﺑ ﻪﮐ ﺖﺳﺍ ﻦﯾﺍ ﻊﺑﺎﺗ ﺩﻮﺧ .ﺩﻮﺷﯽﻣ ﻪﻓﺎﺿﺍ $errors ﻪﯾﺍﺭﺁ ﻪﺑ ﯽﻣﺎﯿﭘ ،ﺩﺭﺍﺩ ﺩﻮﺟﻭ ﯽﻠﮑﺸﻣ ﺮﮔﺍ .ﺪﯿﻨﮐ ﯽﺳﺭﺮﺑ ﺩﺭﻮﻣ ﻦﯾﺍ ﺭﺩ ﺍﺭ ﻦﻣ 101 ﻪﻟﺎﻘﻣ ﻢﻨﮐ ﯽﻣ ﻪﯿﺻﻮﺗ ،ﺪﯿﺘﺴﻫ ﺞﯿﮔ ﯽﻤﮐ ﺮﮔﺍ :ﺩﻮﺑ ﻊ .ﻢﯿﻫﺩ ﻪﻣﺍﺩﺍ ﺪﯿﯾﺎﯿﺑ ،ﺪﯿﺘﺴﻫ ﻦﻣ ﺎﺑ ﺯﻮﻨﻫ ﻪﮐ ﺎﻤﺷ ﻪﻤﻫ ﯼﺍﺮﺑ .ﺩﺭﺍﺩ ﺩﻮﺟﻭ (ﻥﺎﻨﯿﻤﻃﺍ ﺖﯿﻠﺑﺎﻗ ﻥﺪﻧﺎﺳﺭ ﺮﺜﮐﺍﺪﺣ ﻪﺑ ﯼﺍﺮﺑ) ﺎﻬﻧﺁ ﯼﺎﻫ ﯼﺩﻭﺭﻭ ﺪﯿﯾﺄﺗ ﯼﺍﺮﺑ ﺭﺎ .ﺪﻨﺘﺴﻫ ﻢﻬﻣ ﺭﺎﯿﺴﺑ ﮎﺎﻨﺘﺸﺣﻭ ﯼﺎﻫﺎﻄﺧ ﺯﺍ ﯼﺮﯿﮔﻮﻠﺟ ﯼﺍﺮﺑ ﺎﻣﺍ ،ﺪﻨﺘﺴﻫ ﺮﺗ ﻩﺪﯿﭽﯿﭘ ﯽﻤﮐ ﺮﺧﺁ ﺩ .ﺪﯿﻨﮐ ﺏﺎﻨﺘﺟﺍ ﻥﺁ ﺯﺍ ﺖﺳﺍ ﺮﺘﻬﺑ ﻥﺎﮑﻣﺍ ﺕﺭﻮﺻ ﺭﺩ ﻪﮐ ﺖﺳﺍ ﺪﻨﯾﺎﺷﻮﺧﺎﻧ ﺎًﻌﻗﺍﻭ ﯽﮕﺘﻔﺷﺁ ﮏﯾ ﻦﯾﺍ Post Slug ﺪﺷﺎﺑ ﺩﺮﻓ ﻪﺑ ﺮﺼﺤﻨﻣ ﺪﯾﺎﺑ .(ﺪﻧﺍﺩﺮﮔﯽﻣﺮﺑ ﺍﺭ Boolean FALSE ﮏﯾ ﻊﺑﺎﺗ ﻦﯾﺍ) ﺖﺳﺍ ﻩﺪﺸﻧ ﻢﯿﻈﻨﺗ ﺮﯿﻐﺘﻣ ﻭ ﺩﻮﺷ set_page_template() ﻪﺤﻔﺻ ﯼﻮﮕﻟﺍ ﮏﯾ (ﻞﮑﺷ ﻪﺑ my_page_tem add_category() ﯽﻣ ﻥﺎﮑﻣﺍ ﺎﻤﺷ ﻪﺑﻪﯾﺍﺭﺁ ﺪﻫﺩﻪﺘﺳﺩ ﻪﺑ ﺍﺭ ﺢ .ﺩﺮﮐ ﻢﯿﻫﺍﻮﺧ ﻩﺩﺎﻔﺘﺳﺍ search() ﻊﺑﺎﺗ ﺯﺍ ﺭﺎﮐ ﻦﯾﺍ ﯼﺍﺮﺑ .ﻢﯿﻨﮐ ﺍﺪﯿﭘ .ﺩﺮﯾﺬﭘ ﯽﻣ ﺍﺭ ﺮﺘﻣﺍﺭﺎﭘ 2 ﻊﺑﺎﺗ .ﺪﯿﻨﮐ ﺍﺪﯿﭘ «ﺱﺎﺳﺍﺮﺑ ﻮﺠﺘﺴﺟ» ﺭﺍﺪﻘﻣ .1 .ﻢﯿﻨﮐ ﯽﺳﺭﺮﺑ ﺮﺗﻖﯿﻗﺩ ﯽﻤﮐ ﺍﺭ ﻡﺍﺪﮐ ﺮﻫ ﺪﯿﯾﺎﯿﺑ !ﻩﺩﺎﺳ .ﺩﻮﺷ ﯽﻣ ﻩﺮﯿﺧﺫ ﻭ ﻩﺪﻧﺍﺩﺮﮔﺮﺑ (ﺩﻮﺷ ﯽﻣ ﻩﺮﯿﺧﺫ ﻪﮐ ﯽﻤﺋﺍﺩ ﺪﻧﻮﯿﭘ ﻭ ﻪﺳﺎﻨﺷ ﻦﯿﻨﭽﻤﻫ ﻭ) .ﺪﯾﺩ ﺪﯿﻫﺍﻮﺧ ﺍﺭ ﺪﮐ ﻦﯾﺍ :ﺪﯿﺷﺎﺑ ﻪﺘﺷﺍﺩ ﻪﺟﻮﺗ ﺎًﻔﻄﻟ .ﺪﯿﻨﮑﻧ ﻩﺩﺎﻔﺘﺳﺍ ﺵ .ﺩﻮﺷ ﺺﺨﺸﻣ "post" ﻥﺍﻮﻨﻋ ﻪﺑ ﻡﻮﺳ ﺮﺘﻣﺍﺭﺎﭘ ﻪﮑﻨﯾﺍ ﺮﮕﻣ ﺪﻧﺍﺩﺮﮔﯽﻣﺮﺑ ﺍﺭ ﺕﺎﺤﻔﺻ ﻂﻘﻓ ، .ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ Not Found 404 ﯼﺎﻄﺧ ﻥﺁ ﺯﺍ ﺲﭘ .ﺩﻮﺷ ﺍﺮﺟﺍ ﺪﻧﺍﻮﺗﯽﻣ (ﻝﻭﺍ ﺭﺎﺑ) ﺭ !ﻥﺩﻮﺑ ﺭﺬﺣﺮﺑ .ﺪﻨﮐ ﯽﻣ ﺩﺎﺠﯾﺍ ﺍﺭ ﯽﺗﻼﮑﺸﻣ ﻢﻫ ﺎﺑ Slug ﺮﯿﯿﻐﺗ ﻭ ﻮﺠﺘﺴﺟ ،ﻥﺍﻮﻨﻋ ﺱﺎﺳﺍ ﺮﺑ ﻮﺠ ﺖﺳﺍ ﺖﺳﺭﺩ! ﯽﻣ ،ﺩﺭﺍﺪﻧ ﺩﻮﺟﻭ ﻮﺠﺘﺴﺟ ﻥﺎﮑﻣﺍ ﻪﮐ ﻥﻮﻨﮐﺍﻊﺑﺎﺗ ﻪﺑ ﻢﯿ !ﺖﺳﺎﺒﯾﺯ ﺪﮐ ﻦﯾﺍ ﻪﮐ ﻢﻨﮐ ﻑﺍﺮﺘﻋﺍ ﺪﯾﺎﺑ ،ﻢﺘﺴﻫ ﻪﻧﺍﺭﺍﺪﺒﻧﺎﺟ ﯽﻤﮐ ﻦﻣ ﻪﭼﺮﮔﺍ .ﺪﺷﺎﺑ .ﻢﯾﺭﺍﺩ ﺪﯿﻔﻣ ﯽﻣﻮﻤﻋ ﻊﺑﺍﻮﺗ ﺪﻨﭼ ﺎﻣ ،ﺖﯾﺎﻬﻧ ﺭﺩ .ﺖﺳﺍ update() ﯼﺍﺮﺑ ﺎ ؛ﺪﯾﺯﺍﺪﻨﯿﺑ ﯽﻫﺎﮕﻧ .ﺪﯿﻨﮐ ﻪﻓﺎﺿﺍ ﺍﺭ ﻥﺎﻣﺯ ﺭﺩ ﯽﯾﻮﺟ ﻪﻓﺮﺻ/ﺪﯿﻔﻣ ﻊﺑﺎﺗ ﺪﻨﭼ ﻪﮐ ﺖﺳﺍ ﯽﺑﻮﺧ ﻩﺪﯾ ؛ﻊﯾﺮﺳ ﺕﺎﺤﯿﺿﻮﺗ !ﺪﯿﺷﺎﺑ ﻪﺘﻓﺎﯾ ﺮﮕﻨﺷﻭﺭ ﻢﻫ ﻭ ﺪﯿﻔﻣ ﻢﻫ ﺍﺭ ﻥﺁ ﻪﮐ ﻡﺭﺍﻭﺪﯿﻣﺍ :ﻦﻣ ﻪﻟﺎﻘﻣ ﻥﺪﻧﺍﻮﺧ ﯼﺍﺮﺑ ﺎﻤﺷ ﺯ .ﺪﯿﻨﮐ ﺡﺮﻄﻣ ﺮﯾﺯ ﺕﺍﺮﻈﻧ ﺶﺨﺑ ﺭﺩ ﺍﺭ ﺎﻬﻧﺁ ،ﺪﯾﺭﺍﺩ ﯽﻟﺍﻮﺳ ﺮﮔﺍ .(? harribellthomas@ :ﺖﺳﺍ ﻩﺩﺎﻔﺘﺳﺍ
$Poster = new PostController; ﺎﻫ ﯽﮔﮋﯾﻭ ﻢﯿﻈﻨﺗ
$Poster->set_title( 'The Title' );$Poster->set_type( 'page' );$Poster->set_content( '<h1>This is my awesome new post!</h1>' );$Poster->set_author_id( 12 );$Poster->set_post_slug( "new_slug" );$Poster->set_page_template( 'fullwidth_page.php' );[ ‘draft’ | ‘publish’ | ‘pending’| ‘future’ | ‘private’ | custom registered status ]$Poster->set_post_state( "pending" ); ﻥﺩﺮﻛ ﺩﺎﺠﻳﺍ
$Poster->set_title( "New Post" );$Poster->create(); ﻥﺩﺮﮐ ﻮﺠﺘﺴﺟ
$Poster->search( 'SEARCH_BY' , 'DATA' );$Poster->search( 'title' , 'DATA' );$Poster->search( 'id' , 'DATA' );$Poster->search( 'slug' , 'DATA' );$error = $Poster->get_var( 'errors' );
$Poster->PrettyPrint( $error ); ﯽﻧﺎﺳﺭ ﺯﻭﺭ ﻪﺑ
$Poster->update();$Poster->search( 'id', 1 );
$Poster->set_title( 'New Title' );
$Poster->update(); ﺎﻫﺮﯿﻐﺘﻣ ﯽﺑﺎﯾﺯﺎﺑ
$Poster->get_var( 'title' ); // Returns the title (if you have set it)
$Poster->get_var( 'type' ); // Returns the post type (if you have set it)
$Poster->get_var( 'content' ); // Returns the content (if you have set it)
$Poster->get_var( 'category' ); // Returns the category as an array (if set)
$Poster->get_var( 'template' ); // Returns the template (if you have set it)
$Poster->get_var( 'slug' ); // Returns the slug (if you have set it)
$Poster->get_var( 'auth_id' ); // Returns the author id (if you have set it)
$Poster->get_var( 'status' ); // Returns the post's status (if you have set it)
$Poster->get_var( 'errors' ); // Returns the errors array
/** AFTER YOU HAVE EITHER CREATED A POST OR SUCCESSFULLY SEARCHED FOR ONE YOU CAN USE THESE **/
// Returns the a PHP Object of the post. This can be used to check if the search method was successful
$Poster->get_var( 'current_post' );
// Returns the selected post's ID (integer).
$Poster->get_var( 'current_post_id' );
// Returns the URL permalink of the selected post
$Poster->get_var( 'current_post_permalink' ); ﻡﺎﮔ ﻪﺑ ﻡﺎﮔ ﯼﺎﻤﻨﻫﺍﺭ :ﺱﻼﮐ ﺖﺧﺎﺳ
class PostController {
. . . class functions go here . . .
}// Variables for Post Data
public $PC_title;
public $PC_type;
public $PC_content;
public $PC_category;
public $PC_template;
public $PC_slug;
public $PC_auth_id;
public $PC_status = "publish";
// Variables for Post Updating
public $PC_current_post;
public $PC_current_post_id;
public $PC_current_post_permalink;
// Error Array
public $PC_errors;// Creation functions
public function create() {
if ( isset( $this->PC_title ) ) {
if ( $this->PC_type == 'page' ) {
$post = get_page_by_title( $this->PC_title, 'OBJECT', $this->PC_type );
} else {
$post = get_page_by_title( $this->PC_title, 'OBJECT', $this->PC_type );
}
$post_data = array(
'post_title' => wp_strip_all_tags($this->PC_title),
'post_name' => $this->PC_slug,
'post_content' => $this->PC_content,
'post_status' => $this->PC_status,
'post_type' => $this->PC_type,
'post_author' => $this->PC_auth_id,
'post_category' => $this->PC_category,
'page_template' => $this->PC_template
);
if ( ! isset( $post ) ) {
$this->PC_current_post_id = wp_insert_post( $post_data, $error_obj );
$this->PC_current_post = get_post( ( integer ) $this->PC_current_post_id, 'OBJECT' );
$this->PC_current_post_permalink = get_permalink( ( integer ) $this->PC_current_post_id );
return $error_obj;
} else {
$this->update();
$this->errors[] = 'That page already exists. Try updating instead. Control passed to the update() function.';
return FALSE;
}
} else {
$this->errors[] = 'Title has not been set.';
return FALSE;
}
}// SET POST'S TITLE
public function set_title( $name ) {
$this->PC_title = $name;
return $this->PC_title;
}
// SET POST'S TYPE
public function set_type( $type ) {
$this->PC_type = $type;
return $this->PC_type;
}
// SET POST'S CONTENT
public function set_content( $content ) {
$this->PC_content = $content;
return $this->PC_content;
}
// SET POST'S AUTHOR ID
public function set_author_id( $auth_id ) {
$this->PC_auth_id = $auth_id;
return $this->PC_auth_id;
}
// SET POST'S STATE
public function set_post_state( $content ) {
$this->PC_status = $content;
return $this->PC_status;
} // SET POST SLUG
public function set_post_slug( $slug ) {
$args = array('name' => $slug);
$posts_query = get_posts( $args );
if ( ! get_posts( $args ) && ! get_page_by_path( $this->PC_slug ) ) {
$this->PC_slug = $slug;
return $this->PC_slug;
} else {
$this->errors[] = 'Slug already in use.';
return FALSE;
}
}
// SET PAGE TEMPLATE
public function set_page_template( $content ) {
if ( $this->PC_type == 'page' ) {
$this->PC_template = $content;
return $this->PC_template;
} else {
$this->errors[] = 'You can only use template for pages.';
return FALSE;
}
}
// ADD CATEGORY IDs TO THE CATEGORIES ARRAY
public function add_category( $IDs ){
if ( is_array( $IDs ) ) {
foreach ( $IDs as $id ) {
if ( is_int( $id ) ) {
$this->PC_category[] = $id;
} else {
$this->errors[] = '<b>' .$id . '</b> is not a valid integer input.';
return FALSE;
}
}
} else {
$this->errors[] = 'Input specified in not a valid array.';
return FALSE;
}
}set_post_slug()
set_page_template() & add_category()
ﻥﺩﺮﮐﻮﺠﺘﺴﺟ
// Search for Post function
public function search( $by, $data ) {
switch ( $by ) {
// SEARCH FOR POST USING IT'S ID
case 'id' :
if ( is_integer( $data ) && get_post( ( integer ) $data ) !== NULL ) {
$this->PC_current_post = get_post( ( integer ) $data, 'OBJECT' );
$this->PC_current_post_id = ( integer ) $data;
$this->PC_current_post_permalink = get_permalink( (integer) $data );
return TRUE;
} else {
$this->errors[] = 'No post found with that ID.';
return FALSE;
}
break;
case 'title' :
$post = get_page_by_title( $data );
if ( ! $post ) {
$post = get_page_by_title( $data, OBJECT, 'post' );
}
$id = $post->ID;
if ( is_integer($id) && get_post( (integer)$id) !== NULL ) {
$this->PC_current_post = get_post( (integer)$id, 'OBJECT' );
$this->PC_current_post_id = (integer) $id;
$this->PC_current_post_permalink = get_permalink( (integer) $id );
return TRUE;
} else {
$this->errors[] = 'No post found with that title.';
return FALSE;
}
break;
// SEARCH FOR POST USING IT'S SLUG
case 'slug' :
$args = array(
'name' => $data,
'max_num_posts' => 1
);
$posts_query = get_posts( $args );
if ( $posts_query ) {
$id = $posts_query[0]->ID;
} else {
$this->errors[] = 'No post found with that slug.';
}
if ( is_integer($id) && get_post( (integer)$id) !== NULL ) {
$this->PC_current_post = get_post( (integer)$id, 'OBJECT' );
$this->PC_current_post_id = (integer)$id;
$this->PC_current_post_permalink = get_permalink((integer)$id);
return TRUE;
} else {
$this->errors[] = 'No post found with that slug.';
return FALSE;
}
break;
default:
$this->errors[] = 'No post found.';
return FALSE;
break;
}
} ﻪﺳﺎﻨﺷ ﺱﺎﺳﺍ ﺮﺑ ﻮﺠﺘﺴﺟ
ﻥﺍﻮﻨﻋ ﺱﺎﺳﺍ ﺮﺑ ﻮﺠﺘﺴﺟ
$post = get_page_by_title( $data );
if ( ! $post ) {
$post = get_page_by_title( $data, OBJECT, 'post' );
} Slug ﺱﺎﺳﺍ ﺮﺑ ﻮﺠﺘﺴﺟ
ﺖﯾﺮﯾﺪﻣ ﻭ ﯽﻧﺎﺳﺭ ﺯﻭﺭ ﻪﺑ
// Update Post
public function update(){
if ( isset($this->PC_current_post_id ) ) {
// Declare ID of Post to be updated
$PC_post['ID'] = $this->PC_current_post_id;
// Declare Content/Parameters of Post to be updated
if ( isset($this->PC_title ) && $this->PC_title !== $this->PC_current_post->post_title ) {
$PC_post['post_title'] = $this->PC_title;
}
if ( isset( $this->PC_type ) && $this->PC_type !== $this->PC_current_post->post_type ) {
$PC_post['post_type'] = $this->PC_type;
}
if ( isset($this->PC_auth_id) && $this->PC_auth_id !== $this->PC_current_post->post_type ) {
$PC_post['post_author'] = $this->PC_auth_id;
}
if ( isset($this->PC_status) && $this->PC_status !== $this->PC_current_post->post_status ) {
$PC_post['post_status'] = $this->PC_status;
}
if ( isset($this->PC_category) && $this->PC_category !== $this->PC_current_post->post_category ) {
$PC_post['post_category'] = $this->PC_category;
}
if ( isset($this->PC_template )
&& $this->PC_template !== $this->PC_current_post->page_template
&& ( $PC_post['post_type'] == 'page' || $this->PC_current_post->post_type == 'page' )
) {
PC_post['page_template'] = $this->PC_template;
}
if ( isset( $this->PC_slug ) && $this->PC_slug !== $this->PC_current_post->post_name ) {
$args = array('name' => $this->PC_slug);
}
if ( ! get_posts( $args ) && !get_page_by_path( $this->PC_slug ) ) {
$PC_post['post_name'] = $this->PC_slug;
} else {
$errors[] = 'Slug Defined is Not Unique';
}
if ( isset($this->PC_content) && $this->PC_content !== $this->PC_status->post_content ) {
$PC_post['post_content'] = $this->PC_content;
}
wp_update_post( $PC_post );
}
return( $errors );
} ﯽﻓﺎﺿﺍ ﻊﺑﺍﻮﺗ
// General functions
public function get_content() {
if ( isset( $this->PC_current_post->post_content ) ) {
return $this->PC_current_post->post_content;
}
}
public function get_var( $name ) {
$name = 'PC_'.$name;
if ( isset( $this->$name ) ) {
return $this->$name;
}
}
public function unset_all() {
foreach ( get_class_vars(get_class($this)) as $name => $default ) {
$this->$name = $default;
}
}
public function __toString() {
return 'Use the PrettyPrint function to return the contents of this Object. E.g;<pre>$my_post->PrettyPrintAll();</pre>';
}
public function PrettyPrint($data) {
echo "<pre>";
print_r( $data );
echo "</pre>";
}
public function PrettyPrintAll() {
echo "<pre>";
print_r( $this );
echo "</pre>";
}// Start new class
$Poster = new PostController();
// Echoes error message
echo $Poster; !ﺖﺳﺍ ﯼﺪﻨﺑ ﻪﺘﺴﺑ ﮏﯾ ﻦﯾﺍ ﻭ

